Simple asynchronous AJAX communication instance analysis and ajax asynchronous instance analysis

Source: Internet
Author: User

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.

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.