Definition and usage
The Ajax () method loads remote data over an HTTP request.
This method is the jQuery underlying AJAX implementation. Easy to use high-level implementation see $.get, $.post and so on. $.ajax () returns the XMLHttpRequest object that it created. In most cases you do not need to manipulate the function directly unless you need to manipulate the infrequently used options for more flexibility.
In the simplest case, $.ajax () can be used directly without any parameters.
Note: All options can be set globally through the $.ajaxsetup () function.
Grammar
Jquery.ajax ([Settings])
Parameters
Async
Type: Boolean
Default value: True. By default, all requests are asynchronous requests. If you need to send a synchronization request, set this option to false.
Note that the sync request will lock the browser, and the user's other actions must wait for the request to complete before it can be executed.
Data
Type: String
Data sent to the server. is automatically converted to the request string format. The GET request will be appended to the URL. View the ProcessData option description to disallow this automatic conversion. Must be a key/value format. If an array, JQuery automatically corresponds to the same name for the different values. such as {foo:["bar1", "Bar2"]} converted to ' &foo=bar1&foo=bar2 '.
DataType
Type: String
Expected data type returned by the server. If not specified, JQuery is automatically judged intelligently based on the HTTP packet mime information, such as the XML MIME type being recognized as XML. In 1.4, JSON generates a JavaScript object, and script executes it. The data returned by the server is then parsed against this value and passed to the callback function. Available values:
- "XML": Returns an XML document that can be processed with jQuery.
- HTML: Returns plain text HTML information, and the included script tag is executed when the DOM is inserted.
- "Script": Returns plain text JavaScript code. Results are not automatically cached. Unless the "cache" parameter is set. Note: On remote requests (not in the same domain), all POST requests are converted to GET requests. (because the script tag of the DOM will be used to load)
- "JSON": Returns the JSON data.
- "JSONP": Jsonp format. When calling a function using JSONP form, such as "myurl?callback=?" JQuery is automatically replaced? is the correct function name to execute the callback function.
- "Text": Returns a plain text string
Success
Type: Function
The callback function after the request succeeds.
Parameters: The data returned by the server and processed according to the DataType parameter; A string describing the state.
This is an Ajax event.
Type
Type: String
Default value: "GET"). The request method ("POST" or "get"), the default is "get". Note: Other HTTP request methods, such as PUT and DELETE, can also be used, but only some browsers support it.
Url
Type: String
Default value: The current page address. The address where the request was sent.
Error
Type: Function
Default value: Auto-Judgment (XML or HTML). This function is called when a request fails.
There are three parameters: the XMLHttpRequest object, the error message, and (optionally) the exception object that is caught.
If an error occurs, the error message (the second argument) may also be "timeout", "error", "Notmodified", and "ParserError", in addition to being null.
This is an Ajax event.
-
-
Options
-
-
Type: Object
Optional. AJAX request settings. All options are optional.
-
-
-
Beforesend (XHR)
-
-
Type: Function
You can modify the functions of the XMLHttpRequest object before sending the request, such as adding a custom HTTP header.
The XMLHttpRequest object is the only parameter.
This is an Ajax event. If you return False, you can cancel this Ajax request.
-
-
Cache
-
-
Type: Boolean
Default value: False when True,datatype is script and Jsonp. Set to False to not cache this page.
New features of JQuery 1.2.
-
-
Complete (XHR, TS)
-
-
Type: Function
The callback function after the request is completed (called after the request succeeds or fails).
Parameters: XMLHttpRequest object and a string describing the request type.
This is an Ajax event.
-
-
ContentType
-
-
Type: String
Default value: "Application/x-www-form-urlencoded". The content encoding type when sending information to the server.
The default value is appropriate for most cases. If you explicitly pass a content-type to $.ajax () then it will certainly be sent to the server (even if there is no data to send).
-
-
Context
-
Type: Object
This object is used to set the context of the Ajax-related callback function. That is, let the this object in the callback function point (if this parameter is not set, then this points to the options parameter that is passed when the AJAX request is called). For example, you specify a DOM element as the context parameter, so that you set the success callback function to be the DOM element.
Just like this:
function () { $ (this). AddClass ("Done"); });
View Cod
Datafilter
Type: Function
The preprocessing function for the raw data returned by Ajax. Data and type two parameters are provided: Data is the original data returned by Ajax, and type is the DataType parameter supplied when calling Jquery.ajax. The value returned by the function will be further processed by JQuery.
Global
Type: Boolean
Whether to trigger global AJAX events. Default value: True. Setting to FALSE will not trigger global AJAX events, such as Ajaxstart or ajaxstop, which can be used to control different AJAX events.
Ifmodified
Type: Boolean
Get new data only when the server data changes. Default value: False. Use the HTTP packet last-modified header information to determine. In JQuery 1.4, it also checks the server-specified ' ETag ' to determine that the data has not been modified.
Jsonp
Type: String
Overrides the name of the callback function in a JSONP request. This value is used instead of the "callback" part of the URL parameter in the "callback=?" Get or POST request, such as {jsonp: ' onjsonpload '} will cause the "onjsonpload=?" to be passed to the server.
Jsonpcallback
Type: String
Specifies a callback function name for the JSONP request. This value will be used instead of the random function name generated by jQuery automatically. This is primarily used to create a unique function name for JQuery, which makes it easier to manage requests and provides a convenient way to provide callback functions and error handling. You can also specify this callback function name when you want the browser to cache a GET request.
Password
Type: String
Password in response to HTTP access authentication request
ProcessData
Type: Boolean
Default value: True. By default, the data option is passed in, and if it is an object (technically, as long as it is not a string), it will be processed into a query string to match the default content type "application/x-www-form-urlencoded". Set to False if you want to send DOM tree information or other information that you do not want to convert.
Scriptcharset
Type: String
Only if the request is dataType as "Jsonp" or "script", and type is "GET" is used to force the modification of CharSet. Typically used only when local and remote content encodings are different.
Traditional
Type: Boolean
If you want to serialize the data in a traditional way, set it to true. Please refer to the Jquery.param method under the Tools category.
Timeout
Type: Number
Sets the request time-out (in milliseconds). This setting overrides the global settings.
Username
Type: String
The user name used to respond to HTTP access authentication requests.
Xhr
Type: Function
You need to return a XMLHttpRequest object. The default is ActiveXObject under IE and in other cases is xmlhttprequest. Used to override or provide an enhanced XMLHttpRequest object. This parameter is not available until JQuery 1.3.
Sending data to the server
By default, Ajax requests use the GET method. If you want to use the POST method, you can set the type parameter value. This option also affects how content in the data option is sent to the server.
JQuery Ajax-ajax () method