[Add to favorites] jquery Ajax events

Source: Internet
Author: User
Ajax event jquery. Ajax (options) loads remote data through HTTP requests. Jquery underlying Ajax implementation. For easy-to-use high-level implementation, see $. Get, $. Post, and so on.

$. Ajax () returns the created XMLHTTPRequest object. In most cases, you do not need to directly operate on this object, but in special cases it can be used to manually terminate the request.

Note:If you specify the datatype option, make sure that the server returns the correct mime information (for example, XML returns "text/XML "). Incorrect MIME types may cause unpredictable errors. See specifying the data type for Ajax requests.

$. Ajax () has only one parameter: The parameter key/value object, which contains information about various configurations and callback functions. For detailed Parameter options, see.

In jquery 1.2, you can load JSON data across domains. You need to set the data type to jsonp during use. When calling a function in the form of jsonp, such as "myurl? Callback =? "Will jquery be replaced automatically? For the correct function name to execute the callback function. When the data type is set to "jsonp", jquery automatically calls the callback function. Return Value

XMLHttpRequest

Parameters

Options(Optional): Ajax request settings. All options are optional.

Option

Async(Boolean): (default: True) All requests are asynchronous requests by default. To send a synchronization request, set this option to false. Note: The synchronous request locks the browser. Other operations can be performed only after the request is completed.

Beforesend(Function): Before sending a request, you can modify the function of the XMLHTTPRequest object, for example, adding a custom HTTP header. The XMLHTTPRequest object is a unique parameter. Ajax event.

Cache(Boolean): (default: True) The new jquery 1.2 function. Setting it to false will not load request information from the browser cache.

Complete(Function): callback function after the request is complete (called when the request is successful or fails ). Parameter: XMLHTTPRequest object, success information string. Ajax events.

Contenttype(String): (default: "application/X-WWW-form-urlencoded") Content Encoding type when sending information to the server. The default value is applicable to most applications.

Data(Object, string): the data sent to the server. Will be automatically converted to the request string format. The GET request will be appended to the URL. View the description of the processdata option to disable automatic conversion. It must be in key/value format. If it is an array, jquery automatically corresponds to the same name for different values. For example, {FOO: ["bar1", "bar2"]} is converted to '& Foo = bar1 & Foo = bar2 '.

Datatype(String): The expected data type returned by the server. If this parameter is not specified, jquery will automatically return responsexml or responsetext Based on the mime information of the http package and pass it as a callback function parameter. Available values:

"XML": returns an XML document, which can be processed by jquery.

"Html": returns plain text HTML information, including script elements.

"Script": returns plain text JavaScript code. Results are not automatically cached.

"JSON": Return JSON data.

"Jsonp": jsonp format. When calling a function in the form of jsonp, such as "myurl? Callback =? "Will jquery be replaced automatically? For the correct function name to execute the callback function.

 

Error(Function): (default: automatically determines (XML or HTML) The call time when the request fails. Parameter: XMLHTTPRequest object, error message, and (possibly) captured error object. Ajax events.

Global(Boolean): (default: True) Whether to trigger a global Ajax event. Setting false does not trigger global Ajax events. For example, ajaxstart or ajaxstop can be used to control different Ajax events.

Ifmodified(Boolean): (default: false) obtains new data only when the server data changes. Use the last-modified header information of the HTTP packet to determine.

Processdata(Boolean): (default: True) by default, data sent is converted to an object (technically not a string) with the default content type "application/X-WWW-form-urlencoded ". If you want to send the DOM tree information or other information that does not want to be converted, set it to false.

Success(Function): callback function after the request is successful. Parameter: The data returned by the server, in the data format. Ajax events.

Timeout(Number): Set the request timeout (in milliseconds ). This setting overwrites the global setting.

Type(String): (default: "Get") Request Method ("Post" or "get"). The default value is "get ". Note: Other HTTP request methods, such as put and delete, can also be used, but are only supported by some browsers.

URL(String): (default: Current page address) the address of the request sent.

Example

Load and execute a JS file.

Jquery code:

$. Ajax ({
Type: "Get ",
URL: "test. js ",
Datatype: "script"
});

Save the data to the server. If the data is successfully saved, the information is displayed.

Jquery code:

$. Ajax ({
Type: "Post ",
URL: "Some. php ",
Data: "name = John & location = Boston ",
Success: function (MSG ){
Alert ("data saved:" + MSG );
}
});

Load the latest version of an HTML webpage.

Jquery code:

$. Ajax ({
URL: "test.html ",
Cache: false,
Success: function (HTML ){
$ ("# Results"). append (HTML );
}
});

Load data synchronously. The browser is locked when a request is sent. You must use the synchronization method to lock user interaction operations.

Jquery code:

VaR html = $. Ajax ({
URL: "Some. php ",
Async: false
}). Responsetext;

Send XML data to the server. Set processdata to false to Prevent automatic conversion of data formats.

Jquery code:

VaR xmldocument = [create XML document];
$. Ajax ({
URL: "Page. php ",
Processdata: false,
Data: xmldocument,
Success: handleresponse
});
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.