Ajax Code and simple encapsulation

Source: Internet
Author: User

 varxmlhttp = window. XMLHttpRequest?NewXMLHttpRequest ():NewActiveXObject (' microsoft.xmlhttp ');//Create XMLHTTP objects, consider compatibilityXmlhttp.open ("POST", "Ajaxtest.ashx?" + "i=5&j=10",true);//prepare to issue a POST request to the server's GETDATE1.ASHX (get may have a caching problem). No request has been made here yet .Xmlhttp.onreadystatechange =function ()            {                if(Xmlhttp.readystate = = 4)//ReadyState = = 4 indicates that the server has returned the completed data. may have experienced 2 before (request sent, in process), 3 (part of the data in response is available, but the server has not completed a response generation)                {                    if(Xmlhttp.status = = 200)//If the status code is 200, it is successful .{alert (xmlhttp.responsetext); }                    Else{alert ("The AJAX server returned an error! "); }                }            }//do not think if (xmlhttp.readystate = = 4) {Execute before send!!!! Xmlhttp.send ();//only then does the request begin to be sent//when the request is made and the server returns data, it continues to execute downward, so it does not block and the interface is not jammed, which is the meaning of "A" in Ajax "Async". Try to add a sentence of Thread.Sleep (ashx);a simple Ajax package:functionAjax (Url,onsuccess,onfail) {varxmlhttp = window. XMLHttpRequest?NewXMLHttpRequest ():NewActiveXObject (' Microsoft.XMLHTTP '); Xmlhttp.open ("POST", URL,true); Xmlhttp.onreadystatechange=function ()            {                if(Xmlhttp.readystate = = 4)                {                    if(Xmlhttp.status = = 200) {onsuccess (xmlhttp.responsetext); }                    Else{onfail (xmlhttp.status); }}} xmlhttp.send (); //only then does the request begin to be sent}

Ajax Code and simple encapsulation

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.