Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize AJAX simple Asynchronous Communication

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to achieve AJAX simple asynchronous communication". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to achieve AJAX simple asynchronous communication".

The specific analysis is as follows:

Client: sends an empty request to the server.

The code is as follows:

XMLHttpRequestvar xmlHttp;function createXMLHttpRequest () {if (window.ActiveXObject) xmlHttp = new ActiveXObject ("Microsoft.XMLHTTP"); else if (window.XMLHttpRequest) xmlHttp = new XMLHttpRequest ();} function startRequest () {createXMLHttpRequest (); xmlHttp.open ("GET", "9-1.aspx", true); xmlHttp.onreadystatechange = function () {if (xmlHttp.readyState = = 4 & & xmlHttp.status = = 200) alert ("Server returns:" + xmlHttp.responseText) } xmlHttp.send (null);}

Server side: returns a string directly to the client.

The code is as follows:

The copy code is as follows:

Question 1:

Because IE browsers automatically cache the results of asynchronous communications, the results returned by the server are not updated in real time. (but Firefox will refresh normally)

To solve the IE caching problem when asynchronously connecting to the server, change the client code as follows:

Var sUrl = "9-1.aspx?" + new Date () .getTime (); / / address changes xmlHttp.open ("GET", sUrl,true)

Add a millisecond parameter at the end of the accessed server address to make the URL address of each request different, thus deceiving the IE browser to solve the update problem caused by IE cache.

Question 2:

When testing the program, if the client and server are on the same computer, the asynchronous object returns the currently requested http status code status = = 0, so change the client code again as follows:

/ / if (xmlHttp.readyState = = 4 & & xmlHttp.status = = 200) if (xmlhttp.readyState = = 4) {if (xmlhttp.status = = 200 | | / / status==200 indicates success! Xmlhttp.status = = 0) / / when testing locally, status may be 0. Alert ("Server returns:" + xmlHttp.responseText);}

Therefore, the final client code is as follows:

XMLHttpRequestvar xmlHttp;function createXMLHttpRequest () {if (window.ActiveXObject) xmlHttp = new ActiveXObject ("Microsoft.XMLHTTP"); else if (window.XMLHttpRequest) xmlHttp = new XMLHttpRequest ();} function startRequest () {createXMLHttpRequest (); var sUrl = "9-1.aspx?" + new Date (). GetTime (); / / address changes xmlHttp.open ("GET", sUrl,true) XmlHttp.onreadystatechange = function () {/ / if (xmlHttp.readyState = = 4 & & xmlHttp.status = = 200) if (xmlhttp.readyState = = 4) {if (xmlhttp.status = = 200 | | / / status==200 indicates success! Xmlhttp.status = = 0) / / when testing locally, status may be 0. Alert ("Server returns:" + xmlHttp.responseText);}} xmlHttp.send (null);} Thank you for reading, the above is the content of "how to achieve AJAX simple asynchronous communication". After the study of this article, I believe you have a deeper understanding of how to achieve AJAX simple asynchronous communication, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report