How AJAX works-How to Implement Asynchronous and partial refreshing [Implement Code] and ajax Asynchronization
Overriew: If onReadyStateChange is assigned a value by the callback function, it can be called asynchronously. If the callback function operates the DOM directly, local refreshing can be implemented. So how does the onReadyStateChange of XMLHttpRequest know the ready service? How does the status change (Observer mode )? It is implemented by querying the service status (regular polling) on the client.
Details:
1.XMLHttpRequest is responsible for communicating with the server. It has many important internal attributes: readyStatus = 4, status = 200, and so on. When the overall status of XMLHttpRequest is complete (readyStatus = 4), the data has been sent. Then, based on the server's settings (similar to the Request status in which the client polls the server's return status, the Request status is still http short connection, not long connection server push, if everything is ready (status = 200), perform the required operations.
The operation is generally to directly operate the DOM, so AJAX can achieve the so-called "refreshing" user experience.
Document. getElementById ("user1"). innerHTML = "data loading..."; if (xmlhttp. status = 200) {document. write (xmlhttp. responseText );}
2.So how can I achieve Asynchronization on the AJAX client? It is actually the function of the Javascript callback function.
Provides a callback JavaScript function. Once the server response is available, the function is executed.
Business functions:
Function castVote (rank) {var url = "/ajax-demo/static-article-ranking.html"; var callback = processAjaxResponse; executeXhr (callback, url);} functions requiring asynchronous communication: function executeXhr (callback, url) {// branch for native XMLHttpRequest object if (window. XMLHttpRequest) {req = new XMLHttpRequest (); req. onreadystatechange = callback; req. open ("GET", url, true); req. send () (null);} // branch for IE/Windows ActiveX version else if (window. activeXObject) {req = new ActiveXObject ("Microsoft. XMLHTTP "); if (req) {req. onreadystatechange = callback; req. open ("GET", url, true); req. send () ;}} req. onreadystatechange = callbackreq. open ("GET", url, true)
The first line defines the JavaScript callback function, which is automatically executed once the response is ready. The "true" mark specified in req. open () indicates that the request is to be executed asynchronously.
Once the server completes XmlHttpRequest processing and returns it to the browser, the callback method assigned by req. onreadystatechange is automatically called.
Callback function:
function processAjaxResponse() { if (req.readyState == 4) { // only if "OK" if (req.status == 200) { document.getElementById("user1").innerHTML = req.responseText; } else { alert("There was a problem retrieving the XML data:" + req.statusText); } }}
The above AJAX principle-how to achieve asynchronous and partial refreshing [Implementation Code] is all the content shared by the editor. I hope to give you a reference, and I hope you can provide more support for the customer's house.