Jquery.ajax verbose explanation for loading remote data over HTTP requests

Source: Internet
Author: User
Tags browser cache

First, take a look at a jquery.ajax example.

$.ajax ({

Type: "GET",

URL: "/api/searchapi/getresults",

DataType: "JSON",

data:{

Filter: "Test",

Pagenumber:1,

Pagesize:5

},

Success:function (data) {

Do something

},

Complete:function (XMLHttpRequest, Textstatus) {

},

Error:function () {

Request Error Handling

}

});

There are a number of parameters, here are the main list of some common parameters

Pay special attention to the two places inside:

One is data {}

The other is success:function (data) {//do something}

Note that the data in these two places is fundamentally different:

data{} is a data source that is processed in the URL that is passed into this jquery Ajax, and it must be in the format Key/value. If an array, jquery automatically corresponds to the same name for the different values. such as {foo:["bar1", "Bar2"]} will be converted to ' &foo=bar1&foo=bar2 '

Data in Success:function (data) {//do something} returns the result after the server has processed the AJAX request.

There are many more parameters for specific AJAX requests, see below:

Parameter list:

Name of parameter Type Describe
Url String (Default: Current page address) sends the requested address.
Type String (Default: "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.
Timeout Number Sets the request time-out (in milliseconds). This setting overrides the global settings.
Async Boolean (default: 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.
Beforesend 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.
function (XMLHttpRequest) {this  ;//The options for this AJAX request}
Cache Boolean (default: TRUE) JQuery 1.2 new feature, set to False will not load the request information from the browser cache.
Complete Function The callback function after the request completes (called when the request succeeds or fails). Parameters: XMLHttpRequest Object, Success information string.
function (XMLHttpRequest, textstatus) {this  ;//The options for this AJAX request}
ContentType String (Default: "application/x-www-form-urlencoded") the content encoding type when sending information to the server. The default values are suitable for most applications.
Data Object,
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 String

Expected data type returned by the server. If not specified, JQuery automatically returns Responsexml or ResponseText based on the HTTP packet MIME information and is passed as a callback function parameter, with the available values:

"XML": Returns an XML document that can be processed with jQuery.

HTML: Returns plain text HTML information, including the script element.

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

"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.

Error Function (Default: This method is called when a request for automatic judgment (XML or HTML) fails.) This method has three parameters: the XMLHttpRequest object, the error message, and (possibly) the error object being captured.
function (XMLHttpRequest, textstatus, Errorthrown) {  //normally textstatus and Errorthown only one has the value of this   ; Options for this AJAX request}
Global Boolean (default: TRUE) whether to trigger global AJAX events. Setting to FALSE will not trigger global AJAX events, such as Ajaxstart or Ajaxstop. Can be used to control different AJAX events
Ifmodified Boolean (default: false) to get new data only when the server data changes. Use the HTTP packet last-modified header information to determine.
ProcessData Boolean (default: TRUE) by default, the sent data is converted to an object (technically not a 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.
Success Function The callback function after the request succeeds. This method has two parameters: the server returns data, returns the status
function (data, textstatus) {  //data could be xmldoc, jsonobj, HTML, text, etc ...  This The options for this AJAX request}

$.ajax () returns the XMLHttpRequest object that it created. In most cases you do not need to manipulate the object directly, but in special cases you can use it to terminate the request manually.

Note: If you specify the DataType option, make sure that the server returns the correct MIME information (such as XML returns "Text/xml"). The wrong MIME type can cause unpredictable errors.
When you set the datatype type to ' script ', all the remote (not in the same domain) post requests are converted back to get mode.

$.ajax () has only one parameter: The parameter Key/value object, which contains the configuration and callback function information. Detailed parameter options are shown below.

In JQuery 1.2, you can load JSON data across domains and set the data type to JSONP when used. 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. When the data type is set to "Jsonp", JQuery automatically calls the callback function

Jquery.ajax verbose explanation for loading remote data over HTTP requests

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.