Ajax simple Asynchronous communication instance analysis _ajax related

Source: Internet
Author: User
Tags constant

This article illustrates an AJAX approach to simple asynchronous communication. Share to everyone for your reference. The specific analysis is as follows:

client: An empty request is issued to the server.

The code is as follows:

 
 

Server side: Returns a string directly to the client.

The code is as follows:

Copy Code code as follows:
<%@ Page language= "C #" contenttype= "text/html" responseencoding= "gb2312"%>
<%@ Import namespace= "System.Data"%>
<%
Response.Write ("Successful asynchronous test, very happy");
%>

Question one:

Because IE browsers automatically cache the results of the asynchronous communication, the server's return results are not updated in real time. (But Firefox will refresh normally)

To resolve the cache problem of IE when connecting to the server asynchronously, change the client code as follows:

var surl = "9-1.aspx?" + New Date (). GetTime (); Address constant change
xmlhttp.open ("Get", surl,true);

At the end of the server address to add a current time of the number of milliseconds parameter, so that each request URL address is not the same, thereby deceiving IE browser to solve the IE cache caused by the update problem.

Question two:

When testing the program, if both the client and server side are on the same computer, the asynchronous object returns the current requested HTTP status code = 0, and the client code is changed again as follows:

if (xmlhttp.readystate = = 4 && xmlhttp.status = =)
if (xmlhttp.readystate = 4)
{
   if ( Xmlhttp.status = 200 | | STATUS==200 said success!
     Xmlhttp.status = = 0)//The status may be 0 when the test is local.
      alert ("Server returns:" + Xmlhttp.responsetext);
}

As a result, the final client code is as follows:

  
 

I hope this article will help you with your AJAX programming.

Related Article

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.