EN
Este conteúdo não está disponível em seu idioma, mas aqui está a versão em inglês.

XMLHttpRequest object

Represents an XML request using HTTP.

Members

The XMLHttpRequest object has these types of members:

Events

The XMLHttpRequest object has these events.

EventDescription
onreadystatechange

Sets or retrieves the event handler for asynchronous requests.

ontimeout

Raised when there is an error that prevents the completion of the request.

 

Methods

The XMLHttpRequest object has these methods.

MethodDescription
abort

Cancels the current HTTP request.

addEventListener

Registers an event handler for the specified event type.

dispatchEvent

Sends an event to the current element.

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.

overrideMimeType

Sets the Content-Type header for the response to the MIME provided.

removeEventListener

Removes an event handler that the addEventListener method registered.

send

Sends an HTTP request to the server and receives a response.

setRequestHeader

Adds custom HTTP headers to the request.

 

Properties

The XMLHttpRequest object has these properties.

PropertyAccess typeDescription

constructor

Returns a reference to the constructor of an object.

readyState

Retrieves the current state of the request operation.

response

Read-only

Returns the response received from the server.

responseBody

Retrieves the response body as an array of unsigned bytes.

responseText

Retrieves the response body as a string.

responseType

Read/write

Describes the data type of the response associated with the request.

responseXML

Retrieves the response body as an XML DOM object.

status

Retrieves the HTTP status code of the request.

statusText

Retrieves the friendly HTTP status of the request.

timeout

Gets or sets the time-out value.

withCredentials

Read/write

Indicates whether user credentials should be included with the request.

 

Standards information

Remarks

The XMLHttpRequest property is available on the window object.


var oReq = new XMLHttpRequest;

Starting with Internet Explorer 10 in Windows 8, the responseXML property returns a native XML document. This can affect webpages written for earlier versions of Windows Internet Explorer, but makes it more compatible with other browsers.

For previous version of Internet Explorer, the additional step of passing responseText through DOMParser.


var xhr = new XMLHttpRequest();

//...

var parser = new DOMParser();

var doc = parser.parseFromString(xhr.responseText, 'text/xml');

// 'doc' contains a native document in both IE9 and IE10


With Internet Explorer 10 in Windows 8, the use of responseXML simplifies getting a native XML document.


var xhr = new XMLHttpRequest();

//...

var doc = xhr.responseXML;

// 'doc' contains a native document in IE10’s Standards and Quirks document modes

// it contains an MSHTML document in IE9 and in IE10’s compatibility document modes


For more info, see XMLHttpRequest responseXML in Internet Explorer 10.

With the XMLHttpRequest object, clients can retrieve and submit XML data directly to a web server without reloading the document. 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.

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


var oReq = new XMLHttpRequest;

To support versions of Windows 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 an onreadystatechange 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) {
            console.log(oReq.responseText);
        }
    }
}
var oReq = getXMLHttpRequest();
if (oReq != null) {
    oReq.open("GET", "http://localhost/test.xml", true);
    oReq.onreadystatechange = handler;
    oReq.send();
}
else {
    window.console.log("AJAX (XMLHTTP) not supported.");
}

 

 

Mostrar:
© 2014 Microsoft