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