JQueryAjax () method User Guide _ jquery-js tutorial

Source: Internet
Author: User
JQuery. ajax () has powerful functions and many configurable parameters. This article mainly summarizes the precautions for this method. Let's take a closer look at the kids shoes that have the same requirements. We hope to help you with jQuery's simple and powerful selector function and provide great support for Ajax operations. In addition to providing the underlying jQuery. Ajax () method, jQuery also provides the following simple method:

(1) jQuery. get (url, [data], [callback], [type])

(2) jQuery. getJSON (url, [data], [callback])

(3) jQuery. getScript (url, [callback])

(4) jQuery. post (url, [data], [callback], [type])

Because jQuery. ajax () has powerful functions and many configurable parameters, we will summarize the precautions for this method.

1. jQuery. ajax () is requested asynchronously by default. If synchronization is required, set async to false. Because some applications must synchronize the request data. For example, in some Flash and JS interaction applications, to request a JS function, you need to get the returned data immediately. In this case, you must use the synchronous Ajax call method.

2. if Ajax is a Get request, the returned data is generally cached by the browser. If you do not want to be cached, you can set the cache parameter to false; or send a request with a timestamp, in this way, the browser will consider it as a new request and reload data from the server. Of course, requests sent by POST will not be cached.

3. dataType: 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:

(1) "xml": returns an XML document, which can be processed by jQuery.

(2) "html": returns plain text HTML information. The script tag is executed when the dom is inserted.

(3) "script": returns plain text JavaScript code. Results are not automatically cached. Unless the "cache" parameter is set. ''' Note: ''' during remote requests (not in the same domain), all POST requests will be converted to GET requests. (Because the script tag of DOM will be used for loading)

(4) "json": Return JSON data.

(5) "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.

(6) "text": returns a plain text string.

The "script" and "json" settings can solve the cross-origin issue of Ajax.

4. If the server returns a string or value, you can use a normal ajax call.

If the server returns a JSON object, you 'd better use jQuery. getJSON or set ype = json. Because it takes time for the browser to parse the JSON object and directly return the JSON object, saving the parsing time. This can avoid errors that are clearly returned on the server but not obtained by the browser.

5. Ajax calls take time, So we usually put all the processing code after Ajax calls in the callback method. This method cannot be used:

The Code is as follows:


Function getMyPrizeList (){
If (isNotEmpty (uid )){
Var obj = new Object ();
Try {
JQuery. ajax ({type: "GET", url: "someurl", async: false, cache: false, dataType: "script", scriptCharset: "gbk", success: function (json ){
Obj = json;
}
});
} Catch (e ){}
Obj = eval ("(" + obj + ")");
// Alert (obj );
Var str = "";
For (var I in obj)
{
Str + =''+''+ Prizearray [obj [I]. prizeno] +''
Str + =''+ 'CD-KEY:' + obj [I]. cdkey +''
Str + =''+' Term: '+ obj [I]. expiratedate +' pre' +'';
}
JQuery ("# prizelist"). append (str );
}
}

This must be done: Put the processing code into the success function!

The Code is as follows:


Function getMyPrizeList (){
If (isNotEmpty (uid )){
Var obj = new Array ();
Try {
JQuery. ajax ({type: "GET", url: "someurl ",
Cache: false,
DataType: "script ",
ScriptCharset: "gbk ",
Success: function (json ){
Try {
Obj = result;
} Catch (e ){}
JQuery ("# prizelist" ).html ("");
Var str = "";
For (var I = 0; I Str + =''+ Prizearray [obj [I]. prizeno] +'';
Str + ='CD-KEY: '+ obj [I]. cdkey +'';
Str + ='Term: '+ obj [I]. expiratedate +'';
}
JQuery ("# prizelist"). append (str );
}
});
} Catch (e ){}
}
}

6. jQuery. getJSON instance:

The Code is as follows:


// Internal function to load and set the detailed information of the debtor
Function innerShowDetail (){
// Obtain data in JSON format
$. GetJSON ('Load. do ', {id: userId}, function (json ){
// Set value based on key
For (key in json ){
If (key = 'id '){
$ ('# DetailDiv #' + key). val (json [key]);
} Else {
If (json [key] = ''){
// No value is set to null
$ ('# DetailDiv #' + key).html ('');
} Else if (key = 'sex '){
$ ('# DetailDiv #' + key).html (json [key] = '0 '? Female: male ');
} Else if (key = 'group '){
If (json [key]! = Null ){
$ ('# DetailDiv #' + keydomain.html (json [key] ['groupname']);
}
} Else {
$ ('# DetailDiv #' + key).html (json [key]);
}
}
}
// Set the dialog box title and content
$ ('# DetailDiv'). removeAttr ('class ');
Dialog. setTitle ('viewer ['+ json ['username'] +'] ');
Dialog.setcontent(('{detaildiv'{.html ());
});
}

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.