How AJAX works-How to Implement Asynchronous and partial refreshing [Implement Code] and ajax Asynchronization

Source: Internet
Author: User

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.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.