XMLHttpRequest object methods and attributes and AJAX standard implementation examples

Source: Internet
Author: User

The methods and attributes of the XMLHttpRequest object are listed below, marked in red as the most likely

Related methods:

Abort () stops the current request

GetAllResponseHeaders () returns all HTTP Request Response Headers as key/value pairs.

GetResponseHeader ("header") returns the string value of the specified header.

Open ("method", "url") creates a call to the server.

Send (content) sends a request to the server

SetRequestHeader ("header", "value") sets the specified header to the provided value. You must call open () before setting any header ()

Related attributes:

This event processor is triggered when the onreadystatechange status changes. Generally, a JavaScript function is called.

ReadyState Request status 0 = not initialized, 1 = loading, 2 = loaded, 3 = interaction, 4 = completed

ResponseText server response, which is a string

The response of the responseXML server, expressed as XML. This object can be parsed as a DOM object.

Status Server HTTP status code (200 corresponds to OK, 404 corresponds to Not Found (Not Found ),

Corresponding text of statusText HTTP status code (OK or Not Found (Not Found)

A standard XMLHttpRequest object to implement ajax

<Script type = "text/javascript">
Var xmlHttp;

// Function 1: Create an XMLHttpRequest object

Function createXMLHttpRequest (){

// Different IE versions

If (window. ActiveXObject ){

Try {xmlHttp = new ActiveXObject ("Msxml2.XMLHTTP ");}

Catch (e) {xmlHttp = new ActiveXObject ("Microsoft. XMLHTTP ");}

}

// Non-IE

Else if (window. XMLHttpRequest ){

XmlHttp = new XMLHttpRequest ();

}

}
// Function 2: The function to be called

Function startRequest (){

CreateXMLHttpRequest ();

Var url = "XXX. ashx? "+ New Date (). getTime ();

XmlHttp. onreadystatechange = handleStateChange;

XmlHttp. open ("GET", url, true );

XmlHttp. send (null );

}

// Function 3: The Event function triggered when the status changes

Function handleStateChange (){

If (xmlHttp. readyState = 4 ){

If (xmlHttp. status = 200 ){

Var result = xmlHttp. responseText;

Document. getElementById ("validateMessage"). innerText = result;

}

}

}

</Script>

 

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.