Jquery.ajax detailed description and instance code

Source: Internet
Author: User
Tags error handling serialization browser cache

return: XMLHttpRequest
Executing an asynchronous HTTP (Ajax) Request
Jquery.ajax (Settings)
Settingsa set the key/value pair configuration of the Ajax request. All options are optional. You can set an option that defaults to any dollar. Ajaxsetup ().
Async
Default: True

By default, all requests are sent asynchronously (that is, this is the default setting to True). If you need to sync the requirements, set to false option. Cross-domain requests and datatype: "JSONP form" requirements do not support synchronization operations. Note that the synchronization request may temporarily lock the browser, disable any action, and request activation.

Beforesend (XMLHttpRequest)
Pre-session callback modifies XMLHttpRequest object before sending. Using this setting, XMLHttpRequest is passed as a unique parameter to a custom header. This is an AJAX event. You can cancel the request if the function returns false

Cache Boolean
Default: True, for data type ' script ' and ' jsonp ' form fake
If set to false, this will force you to request a page that is not cached by the browser

Complete (XMLHttpRequest, textstatus)
At the end of the call request for the function (executed in a successful and error callback). This function gets two parameters: the XMLHttpRequest object and the status of a string classification request ("Success", "Notmodified", "Error", "timeout" or "ParserError"). This is an AJAX event.

ContentType string
Default: ' application/x-www-form-urlencoded '

Use this content type when sending data to the server. The default is "application/X-WWW profile has been urlencoded", which is the case for most cases. If you pass an explicit content type to. $.ajax (), it will always be sent to the server (even if no data is sent). Data is always sent to the server using the Utf-8 character set, and you must properly decode the server side.

Context
This object will be in all AJAX-related callbacks. For example, in a background that is specified as a DOM element, the callback background is completed for a requirement like this:
$.ajax ({url: "test.html", Context:document.body, Success:function () {
$ (this). AddClass ("Done");
}});

Data
Data is sent to the server. It will be converted to a query string if it is not a string. This is the URL of the request that is attached to get. See the ProcessData option to prevent this automatic processing. Object must be a key/value pair. If the value is an array, the serialization of jquery is based on the value of the same key as the traditional setting (as described below).

Datafilter (data, type)
A function is used to process data xmlhttprequest.this The original reply is a pre filtering function that purifies the response.you should return the disinfection data.the function will get two parameters: Raw data returned from the server, and ' data type ' parameters.


DataType string
Default:intelligent guess (XML, JSON, script, or HTML)

You expect from the server type. If not specified, jquery will intelligently attempt to obtain results based on the response (an XML MIME type will produce 1.4 of JSON will produce a Web effects object XML, in 1.4 script, the script will execute, and any MIME type otherwise will return a string). The available types (and the result of passing as the first parameter to your successful callback):

"XML": Returns an XML file that can be processed through jquery.
HTML: Returns HTML as plain text, including script tags when evaluated, inserted in the DOM.
Script: The computed response is used as a JavaScript statement and returns plain text. Unless you disable the option "cache" cache usage. Note: This translates to a gets position for the request of the remote domain.
"JSON": the response is evaluated as a JSON and a JavaScript object is returned. The JSON data in jquery 1.4 is strictly analyzed, and any malformed JSON is rejected, parsing the error exception. (More about the correct JSON format information json.org.) )
"Jsonp form": Jsonp form under the action of a JSON block load. Will add an extra "? callback =? "Specifies the callback at the end of your URL.
"Text": a plain text string.

Error (XMLHttpRequest, optional) loading feature
A call to a function if the request fails. The function passes three parameters: the XMLHttpRequest object, which describes the type of error that occurred and the optional exception object, if it occurs. The possible values for the second argument (except nulls) are "timeout", "error", "Notmodified", and "ParserError". This is an AJAX event.

Globalboolean
Default: True
Whether this request triggers a global AJAX event handler. The default is true. Set to False to prevent triggering of a global handler like Ajaxstart or Ajaxstop. This can be used to control various AJAX events.

Ifmodifiedboolean
Default: False
Allow requests to be successful only if the response has changed since the last request. This is done by checking the last-modified head. The default value is False and the header is ignored. This technique in jquery 1.4 can also be checked by the "ETag", which is specified by the server to catch the unmodified data.

Jsonps Tutorial Tring
Overrides the name requirement for a JSONP callback function. This value will be used instead of ' callback ' in ' callback ' =? ' part in the URL query string. So (jsonp form: ' onjsonpload ') can cause ' Onjsonpload =? ' passed to the server.

Jsonpcallbackstring
Specifies the requirement for a JSONP callback function name. This value will be used instead of jquery's automatically generated random name. It is best to have jquery generate a unique name because it makes it easier to manage requirements and provides callback and error handling. You may want to specify a callback when you want to better get the browser cache for getting requests.

Passwordstring
The password will be used to respond to HTTP access authentication requests.

Processdataboolean
Default: True
By default, data is passed on as an object (technically, more than any other string) data option will be processed and converted into a query string to accommodate the default content type "application/x-www form that has been urlencoded". If you want to send a domdocument, or other data that is not processed, set the option to false.

Scriptcharsetstring
Only "Jsonp" or "script" data types are used, and the type of "get" is required. Mandatory requirements are interpreted as a set of characters. Only need differences between the remote and local content of the character set.

Success (data, Textstatus, XMLHttpRequest) functions
A call to a function if the request succeeds. This function gets three parameters: The data returned from the server, the format is described by the ' data type ' parameter, a string to describe the state, and the XMLHttpRequest object (available in jquery 1.4). This is an AJAX event.

Timeoutnumber
Sets the local request timeout in milliseconds. This will cover the global timeout if one person passes the $ set. Ajaxsetup. For example, you can use this property to provide a single request for a longer timeout than all other requirements you set for the time in one second. See $. Ajaxsetup Global Timeout ().

Traditionalboolean
Set to True if you want to use the traditional style of the parameter serialization.

Typestring
Default: ' Get '
The type of request ("POST" or "get"), and the default is "get". Note: Other HTTP request methods, such as put and delete, can also be used here, but they are not supported by all browsers.

URLString
Default: Current page
A string that contains the URL to which the request is sent.

Usernamestring
A user name is used to respond to HTTP access authentication requests.

Xhrfunction
Callback to create the XMLHttpRequest object. ActiveXObject (IE) that are available at the time of default, except for XMLHttpRequest. Rewrite to provide your xmlhttprequest or to the factory to enhance your own implementation.

This cost. The $.ajax () function is based on all of the jquery Ajax requests sent. It is often unnecessary to call this function directly, a number of higher-level alternatives like $. Get () and. Load () is available for selection and easier to use. If the uncommon option is required, though, $. $.ajax () can be used more flexibly.

Simply put, the $.ajax () function of the dollar can be invoked without parameters. :

$$.ajax ();. Note: The default setting is to set the global use of the $ ajaxsetup () function.

In this example, the contents of the current page are loaded without the option, but the results are not. To use the result, one of the callback functions that we can implement.

callback function
The Beforesend, error, Datafilter, success and completion of all options are taken at the appropriate time to invoke the callback function. All of these for this object will be passed in the context Property object to Ajax in Settings;. If this is not specified this will be a reference to the Ajax settings themselves. Ajax requests, such as JSONP forms and Cross-domain get requests, do not use certain types of xhr, and in these cases the XMLHttpRequest parameters passed to the callback will be indeterminate.

Beforesend is referred to as a parameter by XMLHttpRequest object, before it is sent as a request.
The so-called error if the request fails. It is the type of error that is represented by a string of XMLHttpRequest, if applicable to the exception object.
Datafilter is called success. It must return (possibly change) the success of the data passing by returning the value of the data and data type.
If successful, it is called a successful request. It is through the returned data, a code that contains success and a XMLHttpRequest object.
When completed, the call request completes either in failure or success. It is through the XMLHttpRequest object, and one contains a success or error code.
In order for the returned HTML to be used, we can implement a successful handler

$.ajax ({
URL: ' ajax/test.html ',
Success:function (data) {
$ ('. Result '). HTML (data);
Alert (' Load was performed. ');
}
});

>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.