On the page, we often encounter local refresh example, this time, we need to use Ajax,
Because a lot of the code is common, so we think of the code encapsulation, simplifying the use, reducing the redundancy
The JavaScript Ajax code is as follows:
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);
In order to implement the top-down operation of the video without refreshing the page, we first encapsulate Ajax, create a new ajax.js, use post to submit
functionAjax (URL, onsuccess) {varxmlhttp = window. XMLHttpRequest?NewXMLHttpRequest ():NewActiveXObject (' microsoft.xmlhttp ');//Create XMLHTTP objects, consider compatibility. XHRXmlhttp.open ("POST", URL,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 . //DRY: Do not copy and paste code //Ajax is asynchronous, not waiting for server-side return to continue executionXmlhttp.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 HTTP status code is 200 it is successful{onsuccess (xmlhttp.responsetext); } Else{alert ("The AJAX server returned an error! "); } } } //do not think if (xmlhttp.readystate = = 4) {Execute before send!!!! Xmlhttp.send ();//This is the time to start sending requests. is not equal to server-side return. Request sent out, I wait! Listen to onReadyStateChange! }
HTML page:
Need to introduce an AJAX function just encapsulated <script type="Text/javascript"Src="Js/ajax.js"></script> <script type="Text/javascript">function Cai () {Ajax ("Zancai.ashx?action=cai", Function (resText) {document.getElementById ("Caicount"). InnerHTML =ResText; }); } </script>"Diaosi.mp4"AutoPlay controls></video> <p><input type="Button"Name="Zan"Value="likes"onclick="Zan ()"/><label id="Zancount"></label></p> <p><input type="Button"Name="Cai"Value="Tread"onclick="Cai ()"/><label id="Caicount"></label></p></body>2. Net nvelocity native JavaScript Ajax encapsulation using