The Ajax and native Ajax methods of jquery

Source: Internet
Author: User

The AJAX approach to jquery:


$.ajax ({
URL: '/comm/test1.php ',
Type: ' POST ',//get
Async:true,//or FALSE, whether asynchronous
data:{
Name: ' Yang ', age:25
},
timeout:5000,//Time-out
DataType: ' json ',//Return data format: Json/xml/html/script/jsonp/text
Beforesend:function (XHR) {
Console.log (XHR)
Console.log (' before sending ')
},
Success:function (DATA,TEXTSTATUS,JQXHR) {
Console.log (data)
Console.log (Textstatus)
Console.log (JQXHR)
},
Error:function (xhr,textstatus) {
Console.log (' ERROR ')
Console.log (XHR)
Console.log (Textstatus)
},
Complete:function () {
Console.log (' End ')
}
})

Native Ajax methods:

$ (' #send '). Click (function () {
5 Stages of the request, corresponding to the value of the readystate
0: Uninitialized, the Send method is not called;
1: Sending the request, the Send method has been called;
2: After the request is sent, the Send method is executed;
3: The response content is being parsed;
4: Response content parsing completed;

var data = ' Name=yang ';
var xhr = new XMLHttpRequest (); Create an Ajax object
Xhr.onreadystatechange = function (event) {//Listening to Ajax objects
if (xhr.readystate = = 4) {//4 means parsing is complete
if (Xhr.status = = 200) {//200 for normal return
Console.log (XHR)
}
}
};
Xhr.open (' POST ', ' url ', true); Establish connection, parameter one: Send method, two: Request address, Third: asynchronous, true Async
Xhr.setrequestheader (' Content-type ', ' application/x-www-form-urlencoded '); Dispensable
Xhr.send (data); Send
});

The Ajax and native Ajax methods of jquery

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.