Simple asynchronous AJAX communication instance analysis and ajax asynchronous instance analysis
This article describes the simple asynchronous AJAX communication method. Share it with you for your reference. The specific analysis is as follows:
Client:Send an empty request to the server.
The Code is as follows:
<Html>
Server:Returns a string directly to the client.
The Code is as follows:
Copy codeThe Code is as follows: <% @ Page Language = "C #" ContentType = "text/html" ResponseEncoding = "gb2312" %>
<% @ Import Namespace = "System. Data" %>
<%
Response. Write ("Asynchronous test successful, very happy ");
%>
Question 1:
The IE browser automatically caches the asynchronous communication results and does not update the server's returned results in real time. (But Firefox will refresh normally)
To solve the IE cache problem during asynchronous connection to the server, change the client code as follows:
Var sUrl = "9-1.aspx? "+ New Date (). getTime (); // The address is constantly changing xmlHttp. open (" GET ", sUrl, true );
Add a millisecond parameter of the current time at the end of the 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 a program, if both the client and the server are on the same computer, the asynchronous object returns the http status code of the current request status = 0, so the client code is changed 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) // during local testing, the status may be 0. Alert ("server return:" + xmlHttp. responseText );}
The final client code is as follows:
<Html>
I hope this article will help you with Ajax programming.