Use XMLHTTP to implement communication with the server without refreshing traffic

Source: Internet
Author: User
XMLHTTP is of a lot of use. Here we only need to talk about communication with the server without refreshing it. Let's look at this Code:

Code:
<Script language = "JavaScript">
Xml = new activexobject ("Microsoft. XMLHTTP ");
VaR post = ""; // construct the data to carry
XML. open ("Post", "http: // localhost: 7001/testwl/index. JSP ", false); // use the POST method to open a connection to the server and communicate asynchronously.
XML. setRequestHeader ("Content-Length", post. Length );
XML. setRequestHeader ("Content-Type", "application/X-WWW-form-urlencoded ");
XML. Send (post); // send data
VaR res = xml. responsetext; // receives data from the server
Document. Write (RES );
</SCRIPT>

Suddenly, this code is used to open an HTTP connection and pass data in standard HTTP format. If you like it, you can use XML format to transfer data. You can change the XML Object Construction Method to be compatible with Mozilla and Netscape. Next, we will write a round robin to refresh the user list every time. Of course, we do not need to refresh the page:
Code:
<HTML>
<Head> <title> detector </title>
<Script language = "JavaScript">
Function detect ()
{
Xml = new activexobject ("Microsoft. XMLHTTP ");
VaR post = ""; // construct the data to carry
XML. open ("Post", "http: // localhost: 7001/testwl/index. JSP ", false); // use the POST method to open a connection to the server and communicate asynchronously.
XML. setRequestHeader ("Content-Length", post. Length );
XML. setRequestHeader ("Content-Type", "application/X-WWW-form-urlencoded ");
XML. Send (post); // send data
VaR res = xml. responsetext; // receives data from the server
List. innertext = res;
SetTimeout ("detect ()", 5000); // Round Robin every 5 seconds
}
</SCRIPT>
<Body onload = "detect ()">
<A id = "list"> </a>
</Body>
</Html>

This communication method has a small amount of data, so you don't have to re-transmit the entire page. It takes five seconds to go round, so that normal PCs can withstand a large number of online messages. Constructing a detector to listen to online lists and messages has a very good effect. Even if your customers sit in front of the computer and do not touch the mouse, they can ensure real-time data transmission, page does not jump or refresh.

Session listening and XMLHTTP communication make it easy to develop a well-developed intra-site messaging system. '

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.