syntax
jQuery.ajax( url [, settings ] ) Simple example $.ajax({ url: "process.php", success: function(result){ $("#div1").html(result); } }); eg:-1 Save some data to the server and notify the user once it's complete. $.ajax({ type: "POST", url: "some.php", data: { name: "John", location: "Boston" } }) .done(function( msg ){ alert( "Data Saved: " + msg ); }); eg:- 2 Retrieve the latest version of an HTML page. $.ajax({ url: "test.html", cache: false }) .done(function( html ) { $( "#results" ).append( html ); }); eg:-3 $.ajax({ url: "http://fiddle.jshell.net/favicon.png", beforeSend: function( xhr ) { xhr.overrideMimeType( "text/plain; charset=x-user-defined" ); } }) .done(function( data ) { if ( console && console.log ) { console.log( "Sample of data:", data.slice( 0, 100 ) ); } });
url
depends on DataType
)accepts
setting needs modification, it is recommended to do so once in the $.ajaxSetup()
method.true
)true
by default). If you need synchronous requests, set this option to false
. Cross-domain requests and dataType: "jsonp"
requests do not support synchronous operation. Note that synchronous requests may temporarily lock the browser, disabling any actions while the request is active. As of jQuery 1.8, the use of async: false
with jqXHR ($.Deferred
) is deprecated; you must use the success/error/complete callback options instead of the corresponding methods of the jqXHR object such as jqXHR.done()
or the deprecated jqXHR.success()
.false
in the beforeSend
function will cancel the request. As of jQuery 1.5, the beforeSend
option will be called regardless of the type of request.true, false for dataType 'script' and 'jsonp'
)false
, it will force requested pages not to be cached by the browser. Note: Setting cache
to false will only work correctly with HEAD and GET requests. It works by appending “_={timestamp}” to the GET parameters. The parameter is not needed for other types of requests, except in IE8 when a POST is made to a URL that has already been requested by a GET.success
and error
callbacks are executed). The function gets passed two arguments: The jqXHR (in jQuery 1.4.x, XMLHTTPRequest) object and a string categorizing the status of the request ("success"
, "notmodified"
, "error"
, "timeout"
, "abort"
, or "parsererror"
). As of jQuery 1.5, the complete
setting can accept an array of functions. Each function will be called in turn. This is an Ajax Event.'application/x-www-form-urlencoded; charset=UTF-8'
)$.ajax()
, then it is always sent to the server (even if no data is sent). The W3C XMLHttpRequest specification dictates that the charset is always UTF-8; specifying another charset will not force the browser to change the encoding.This object will be made the context of all Ajax-related callbacks. By default, the context is an object that represents the ajax settings used in the call ($.ajaxSettings
merged with the settings passed to $.ajax
). For example, specifying a DOM element as the context will make that the context for the complete
callback of a request, like so:
$.ajax({ url: "test.html", context: document.body }).done(function() { $( this ).addClass( "done" ); });
{"* text": window.String, "text html": true, "text json": jQuery.parseJSON, "text xml": jQuery.parseXML}
)false for same-domain requests, true for cross-domain requests
)true
. This allows, for example, server-side redirection to another domain. (version added: 1.5)processData
option to prevent this automatic processing. Object must be Key/Value pairs. If value is an Array, jQuery serializes multiple values with same key based on the value of the traditional
setting (described below).Intelligent Guess (xml, json, script, or html)
)The type of data that you’re expecting back from the server. If none is specified, jQuery will try to infer it based on the MIME type of the response (an XML MIME type will yield XML, in 1.4 JSON will yield a JavaScript object, in 1.4 script will execute the script, and anything else will be returned as a string). The available types (and the result passed as the first argument to your success callback) are:
cache
option is set to true
. Note: This will turn POSTs into GETs for remote-domain requests.null
or {}
instead. (See json.org for more information on proper JSON formatting.)cache
option is set to true
.null
) are "timeout"
, "error"
, "abort"
, and "parsererror"
. When an HTTP error occurs, errorThrown
receives the textual portion of the HTTP status, such as “Not Found” or “Internal Server Error.” As of jQuery 1.5, the error
setting can accept an array of functions. Each function will be called in turn. Note: This handler is not called for cross-domain script and cross-domain JSONP requests. This is an Ajax Event.true
)true
. Set to false
to prevent the global handlers like ajaxStart
or ajaxStop
from being triggered. This can be used to control various Ajax Events.{}
)X-Requested-With: XMLHttpRequest
is always added, but its default XMLHttpRequest
value can be changed here. Values in the headers
setting can also be overwritten from within the beforeSend
function. (version added: 1.5)false
)false
, ignoring the header. In jQuery 1.4 this technique also checks the ‘etag’ specified by the server to catch unmodified data.depends on current location protocol
)file
, *-extension
, and widget
. If the isLocal
setting needs modification, it is recommended to do so once in the $.ajaxSetup()
method. (version added: 1.5.1){jsonp:'onJSONPLoad'}
would result in 'onJSONPLoad=?'
passed to the server. As of jQuery 1.5, setting the jsonp
option to false
prevents jQuery from adding the “?callback” string to the URL or attempting to use “=?” for transformation. In this case, you should also explicitly set the jsonpCallback
setting. For example, { jsonp: false, jsonpCallback: "callbackName" }
jsonpCallback
is set to the return value of that function.true
)false
.charset
attribute on the script tag used in the request. Used when the character set on the local page is not the same as the one on the remote script.{}
)An object of numeric HTTP codes and functions to be called when the response has the corresponding code. For example, the following will alert when the response status is a 404:
1
2
3
4
5
6
7
|
$.ajax({ statusCode: { 404: function() { alert( "page not found" ); } } }); |
If the request is successful, the status code functions take the same parameters as the success callback; if it results in an error (including 3xx redirect), they take the same parameters as the error
callback.
(version added: 1.5)
dataType
parameter; a string describing the status; and the jqXHR
(in jQuery 1.4.x, XMLHttpRequest) object. As of jQuery 1.5, the success setting can accept an array of functions. Each function will be called in turn. This is an Ajax Event.$.ajax
call is made; if several other requests are in progress and the browser has no connections available, it is possible for a request to time out before it can be sent. In jQuery 1.4.x and below, the XMLHttpRequest object will be in an invalid state if the request times out; accessing any object members may throw an exception. In Firefox 3.0+ only, script and JSONP requests cannot be cancelled by a timeout; the script will run even if it arrives after the timeout period.true
if you wish to use the traditional style of param serialization.'GET'
)The current page
)ActiveXObject when available (IE), the XMLHttpRequest otherwise
)An object of fieldName-fieldValue pairs to set on the native XHR
object. For example, you can use it to set withCredentials
to true
for cross-domain requests if needed.
$.ajax({ url: a_cross_domain_url, xhrFields: { withCredentials: true } });
In jQuery 1.5, the withCredentials
property was not propagated to the native XHR
and thus CORS requests requiring it would ignore this flag. For this reason, we recommend using jQuery 1.5.1+ should you require the use of it.