Ajax example of a simple client calling XMLHTTP

Source: Internet
Author: User
An example of a simple client calling XMLHTTP.

The client calls XMLHTTP in five steps:
1. Create an XMLHTTP object
2. Open the connection with the server, and define the command sending method, Service webpage (URL), and request permissions.
The client opens a connection to the Service webpage of the server through the open command. Like normal HTTP Command Transmission, you can use the "get" method or "Post" method to direct to the Service webpage of the server.
3. Send commands.
4. Wait for and receive the processing results returned by the server.
5. Release the XMLHTTP object <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> XMLHTTP test </title>
<SCRIPT type = "text/JavaScript" Language = "JavaScript">
Function JB (){
VaR A = NULL;
Try {
A = new activexobject ("msxml2.xmlhttp ");
}
Catch (e ){
Try {
A = new activexobject ("Microsoft. XMLHTTP ");
}
Catch (OC ){
A = NULL;
}
}

If (! A & typeof XMLHttpRequest! = "Undefined "){
A = new XMLHttpRequest ();
}
Return;
}

VaR Req = NULL;
 
Function dorequest (){
Req = NULL;
// Create an XMLHTTP object
Req = JB ();

// If not supported by the browser, an error message is displayed.
If (! REQ ){
Alert ("giving up. Cannot create an XMLHTTP instance .");
Return false;
}

// Define the callback function used to process received data
Req. onreadystatechange = callback;

/******* This part uses the POST method for submission ******/
// Submit the parameter content
VaR strdata = "code = 123 ";
// Target location for data submission
VaR url = "Default. asp ";
// Submit the parameter code = 123 to default. jsp for processing using the POST method
Req. Open ("Post", URL, true );
Req. setRequestHeader ("Content-Type", "application/X-WWW-form-urlencoded"); // req. setRequestHeader ("Content-Type", "application/X-WWW-form-urlencoded; charset = UTF-8"); // UTF-8 encoding

Req. Send (strdata );
/*************************************** ****/

/****** This section uses the get Method for submission ******
// Submit the parameter content
VaR strdata = "code = 123 ";
// The target location for data submission, and add the parameter to the end of the URL in get Mode
VaR url = "Default. asp? "+ Strdata;
// In the get method, request URL: Default. asp? Code = 123
Req. Open ("get", URL, true );
Req. Send (null );
/*************************************** ****/
}

Function callback (){
/* The readystate attribute reflects the progress of the server when processing the request.
0 = uninitialized
1 = Loading
2 = loaded
3 = interactive
4 = complete
*/
If (req. readystate = 4 ){
/* HTTP status code (I. e., 200,404,500, etc .)
Complete Status values can be found in W3C list: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
We only care about the corresponding OK value of 200
*/
If (req. Status = 200 ){
Showresponse ();
} Else {
Alert ("there was a problem with the request .");
}
}
}
Function showresponse (){
/* Receiving methods of several return values:
Responsebody: returns an unsigned integer array.
Responsestream: The returned result is an istream stream.
Responsetext: returns a string.
Responsexml: The returned result is in XML format.
*/
Alert (req. responsetext );
}
</SCRIPT>
</Head>
<Body>
<Input type = "button" name = "button" value = "Send request" onclick = "dorequest ()"/>
</Body>
</Html> // default. ASP: // request for post on the server. form ("Code") receives GET requests. querystring ("Code ")

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.