XMLHttpRequest Object Usage

Source: Internet
Author: User

XMLHttpRequest is?

The user exchanges data with the server in the background.

You can update the Web page without reloading the page;

Request data from the server after the page has been loaded;

Receiving data from the server after the page has been loaded;

Sends data to the server in the background.

All modern browsers support XMLHttpRequest objects.

Create XMLHttpRequest

<script text= "Text/script" >varXMLHTTP;functionloadxmldoc (URL) {XMLHTTP=NULL;if(window. XMLHttpRequest) {//code for all new browsersxmlhttp=NewXMLHttpRequest (); }Else if(window. ActiveXObject) {//code for IE5 and IE6xmlhttp=NewActiveXObject ("Microsoft.XMLHTTP"); }if(xmlhttp!=NULL) {Xmlhttp.onreadystatechange=State_change; Xmlhttp.open ("GET", URL,true); Xmlhttp.send (NULL); }Else{alert ("Your Browser does not support XMLHTTP."); }}functionState_change () {if(xmlhttp.readystate==4)  {//4 = "Loaded"  if(xmlhttp.status==200)    {//K = OK    //... our code here ...    }  Else{alert ("Problem retrieving XML data"); }  }}</script>

Note: onReadyStateChange is an event handle. Its value (state_change) is the name of a function that fires when the state of the XMLHttpRequest object changes. The status changes from 0 (uninitialized) to 4 (complete). We execute the code only if the status is 4 o'clock.

Why use Async=true?

Our instance uses "true" in the third argument of open ().

This parameter specifies whether the request is processed asynchronously.

True indicates that the script will continue to execute after the Send () method without waiting for a response from the server.

The onReadyStateChange event complicates the code. But this is the safest way to prevent code from stopping without getting a response from the server.

By setting this parameter to "false", you can omit the extra onreadystatechange code. This parameter can be used if the execution of the remaining code does not matter if the request fails.

Receive text

Xmlhttp.responsetext;

Convert the custom text to XML

if (window. Domparser) {     new  domparser ();      = parser.pasefromstring (txt, "text/xml");} Else {    new ActiveXObject ("Microsoft.XMLDOM");     false ;    Xmldoc.loadxml (TXT);}

Receive XML

Xmlhttp.responseXML.documentElement.getElementsByTagName ("");

Request Head

Xmlhttp.getallresponseheaders ();

Request to specify head

Xmlhttp.getresponseheader ("");

Is the XMLHttpRequest object a standard for the audience?

XMLHttpRequest objects are not specified in any recommended standards.

However, the "Load and Save" specification of the Web DOM level 3 contains some similar functionality, but no browsers have implemented them.

XMLHttpRequest Object Usage

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.