Jquery $. ajax usage

Source: Internet
Author: User

Copy codeThe Code is as follows: $. ajax ({
Type: "GET ",
Url: "Services/EFService. svc/Members ",
Data :"{}",
ContentType: "application/json; charset = UTF-8 ",
DataType: "json ",
Success: function (data ){
// Play with returned data in JSON format
},
Error: function (msg ){
Alert (msg );
}
});

A global event is triggered by every Ajax request. It broadcasts all the elements in the DOM. The script loaded in the preceding getScript () example is a global Ajax event. Global events can be defined as follows:Copy codeThe Code is as follows: $ ("# loading"). ajaxStart (function (){
$ (This). show ();
});

We can disable global events in a specific request, as long as the global option is set:Copy codeThe Code is as follows: $. ajax ({
Url: "test.html ",
Global: false, // disable global Ajax events .//...});

The following is a complete list of Ajax events officially provided by jQuery:
• AjaxStart (Global Event)
This event is broadcast if an Ajax request is started and no other Ajax requests are currently running.
• BeforeSend (Local Event)
This event, which is triggered before an Ajax request is started, allows you to modify the XMLHttpRequest object (setting additional headers, if need be .)
• AjaxSend (Global Event)
This global event is also triggered before the request is run.
• Success (Local Event)
This event is only called if the request was successful (no errors from the server, no errors with the data ).
• AjaxSuccess (Global Event)
This event is also only called if the request was successful.
• Error (Local Event)
This event is only called if an error occurred with the request (you can never have both an error and a success callback with a request ).
• AjaxError (Global Event)
This global event behaves the same as the local error event.
• Complete (Local Event)
This event is called regardless of if the request was successful, or not. You will always receive a complete callback, even for synchronous requests.
• AjaxComplete (Global Event)
This event behaves the same as the complete event and will be triggered every time an Ajax request finishes.
• AjaxStop (Global Event)
This global event is triggered if there are no more Ajax requests being processed.
JQuery. ajaxSetup (options): sets the global AJAX default options.
Set the default address of an AJAX request to "/xmlhttp/". Do not trigger a global AJAX event. Use POST instead of the default GET method. No option parameters are set for subsequent AJAX requests.
JQuery code:Copy codeThe Code is as follows: $. ajaxSetup ({
Url: "/xmlhttp /",
Global: false,
Type: "POST"
});
$. Ajax ({data: myData });

If it is specified as html, any embedded JavaScript will be executed before HTML is returned as a string. Similarly, if the script type is specified, the server will first generate JavaScript and then return the script as a text data.
JSON data is a structured data that can be easily parsed using JavaScript. If the obtained data file is stored on a remote server (the domain name is different, that is, cross-Origin data retrieval), The jsonp type is required. If this type is used, a query string parameter callback =? will be created? This parameter is added after the request URL. The server should add the callback function name before the JSON data to complete a valid JSONP request. If you want to specify the callback function parameter name to replace the default callback, you can set the jsonp parameter of $. ajax.

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.