jQuery ajax参数详解,还是官网的靠谱!
depends on DataType
)
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"
,
"nocontent"
,
"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). As of jQuery 1.6 you can pass
false
to tell jQuery to not set any content type header.
Note: The W3C XMLHttpRequest specification dictates that the charset is always UTF-8; specifying another charset will not force the browser to change the encoding.
Note: For cross-domain requests, setting the content type to anything other than
application/x-www-form-urlencoded
,
multipart/form-data
, or
text/plain
will trigger the browser to send a preflight OPTIONS request to the server.
$.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:
1
2
3
4
5
6
|
$.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)
)
"xml"
: Returns a XML document that can be processed via jQuery."html"
: Returns HTML as plain text; included script tags are evaluated when inserted in the DOM."script"
: Evaluates the response as JavaScript and returns it as plain text. Disables caching by appending a query string parameter, _=[TIMESTAMP]
, to the URL unless the cache
option is set to true
. Note: This will turn POSTs into GETs for remote-domain requests."json"
: Evaluates the response as JSON and returns a JavaScript object. Cross-domain "json"
requests are converted to "jsonp"
unless the request includes jsonp: false
in its request options. The JSON data is parsed in a strict manner; any malformed JSON is rejected and a parse error is thrown. As of jQuery 1.9, an empty response is also rejected; the server should return a response of null
or {}
instead. (See json.org for more information on proper JSON formatting.)"jsonp"
: Loads in a JSON block using JSONP. Adds an extra "?callback=?"
to the end of your URL to specify the callback. Disables caching by appending a query string parameter, "_=[TIMESTAMP]"
, to the URL unless the cache
option is set to true
."text"
: A plain text string."text xml"
for the dataType. You can also make a JSONP request, have it received as text, and interpreted by jQuery as XML: "jsonp text xml"
. Similarly, a shorthand string such as "jsonp xml"
will first attempt to convert from jsonp to xml, and, failing that, convert from jsonp to text, and then from text to xml.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.
'GET'
)
"POST"
,
"GET"
,
"PUT"
).
(version added: 1.9.0)
true
)
data
option as an object (technically, anything other than a string) will be processed and transformed into a query string, fitting to the default content-type "application/x-www-form-urlencoded". If you want to send a DOMDocument, or other non-processed data, set this option to
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.
dataType
parameter or the
dataFilter
callback function, if specified; 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'
)
method
. You should use
type
if you're using versions of jQuery prior to 1.9.0.
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.
1
2
3
4
5
6
|
$.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.
The $.ajax()
function underlies all Ajax requests sent by jQuery. It is often unnecessary to directly call this function, as several higher-level alternatives like $.get()
and .load()
are available and are easier to use. If less common options are required, though, $.ajax()
can be used more flexibly.
At its simplest, the $.ajax()
function can be called with no arguments:
1
|
$.ajax();
|
Note: Default settings can be set globally by using the $.ajaxSetup()
function.
This example, using no options, loads the contents of the current page, but does nothing with the result. To use the result, you can implement one of the callback functions.