Развернуть
XMLHttpRequest Object

Represents an XML request usingHTTP.

Members Table

The following table lists the members exposed by the XMLHttpRequest object.

Attributes/Properties

PropertyDescription
constructor New for Windows Internet Explorer 8  Returns a reference to the constructor of an object.
readyState Retrieves the current state of the request operation.
responseBody Retrieves the response body as an array of unsigned bytes.
responseText Retrieves the response body as a string.
responseXML Retrieves the response body as an XML Document Object Model (DOM) object.
status Retrieves the HTTP status code of the request.
statusText Retrieves the friendly HTTP status of the request.
timeout New for Internet Explorer 8  Gets or sets the time-out value.

Events

EventDescription
onreadystatechange Sets or retrieves the event handler for asynchronous requests.
ontimeout New for Internet Explorer 8  Raised when there is an error that prevents the completion of the request.

Methods

MethodDescription
abort Cancels the current HTTP request.
getAllResponseHeaders Returns the complete list of response headers.
getResponseHeader Returns the specified response header.
open Assigns method, destination URL, and other optional attributes of a pending request.
send Sends an HTTP request to the server and receives a response.
setRequestHeader Adds custom HTTP headers to the request.

Prototypes

ObjectDescription
XMLHttpRequest Constructor New for Internet Explorer 8  Defines the properties and methods inherited by objects in the XMLHttpRequest Constructor prototype chain.

Remarks

The XMLHttpRequest property is available on the window object in Internet Explorer 7.


var oReq = new XMLHttpRequest;

With the XMLHttpRequest object, Internet Explorer clients can retrieve and submit XML data directly to a Web server without reloading the page. To convert XML data into renderable HTML content, use the client-side XML DOM or Extensible Stylesheet Language Transformations (XSLT) to compose HTML elements for presentation.

The native scripting object also supports the use of expandos (custom properties), and properly recognizes the 'this' notation of JavaScript.

To support versions of Internet Explorer prior to Internet Explorer 7, use the following function to get the XMLHttpRequest object:


function getXMLHttpRequest() 
{
    if (window.XMLHttpRequest) {
        return new window.XMLHttpRequest;
    }
    else {
        try {
            return new ActiveXObject("MSXML2.XMLHTTP.3.0");
        }
        catch(ex) {
            return null;
        }
    }
}

Examples

The following script demonstrates how to create and use the XMLHttpRequest object. For best client-side performance, the XMLHTTP request is asynchronous and uses anonreadystatechange event handler to process the data returned by the call. The script uses the getXMLHttpRequest() function defined above to create the request object.


function handler()
{
    if (oReq.readyState == 4 /* complete */) {
        if (oReq.status == 200) {
            alert(oReq.responseText);
        }
    }
}

var oReq = getXMLHttpRequest();

if (oReq != null) {
    oReq.open("GET", "http://localhost/test.xml", true);
    oReq.onreadystatechange = handler;
    oReq.send();
}
else {
    window.alert("AJAX (XMLHTTP) not supported.");
}

The following example demonstrates the same functionality in Microsoft Visual Basic Scripting Edition (VBScript).


<script type="text/vbscript">
Dim objXML
Function objXML_onreadystatechange()
    If (objXML.readyState = 4) Then
        If (objXML.status = 200) Then
            MsgBox objXML.responseText, 0, objXML.statusText
        End If
    End If
End Function

Function Window_onload()
    If IsObject(window.XmlHttpRequest) Then
        objXML = window.XmlHttpRequest
    Else
        Set objXML = CreateObject("MSXML2.XMLHTTP.3.0")
    End If

    objXML.Open "GET", "http://localhost/test.xml", True
    objXML.OnReadyStateChange = GetRef("objXML_onreadystatechange")
    objXML.Send
End Function
</script> 

In Internet Explorer 8, the VBScript syntax is not supported, which complicates native support detection somewhat. One way to detect Internet Explorer 8 is by using conditional comments. The following comment detects Internet Explorer 8 when it is not running in compatibility mode. The script sets a global variable that can be checked later when creating the XMLHttpRequest object.


<!--[if IE 8]><script type="text/vbscript">
vbIE8 = True
</script><![endif]-->

If Not vbIE8 And IsObject(window.XmlHttpRequest) Then ...
  

Standards Information

This object is defined in The XMLHttpRequest Object (W3C Working Draft) World Wide Web link.

Содержимое сообществаДобавить
Page view tracker