XML DOM - XMLHttpRequest Object

XMLHttpRequest Object

The XMLHttpRequest object provides full access to the HTTP protocol, including the ability to make POST and HEAD requests as well as ordinary GET requests. XMLHttpRequest can return the response from the web server synchronously or asynchronously, and can return content in the form of text or a DOM document.

Although named XMLHttpRequest, it is not limited to being used with XML documents: it can receive any form of text document.

the XMLHttpRequest object is named AJAX a key feature of the Web application architecture.

Browser Support

XMLHttpRequest is well-supported by all modern browsers. The only browser dependency involves the creation of the XMLHttpRequest object. In IE 5 and IE 6, it is necessary to use the IE-specific ActiveXObject() constructor function. As in XMLHttpRequest Object The section introduced.

W3C Standardization

The XMLHttpRequest object has not been standardized, but W3C has started the standardization work. The content of this manual is based on the standardized working draft.

The current XMLHttpRequest implementation is quite consistent. However, there are subtle differences from the standard. For example, one implementation may return null, while the standard requires an empty string, or the implementation may set readyState to 3 without guaranteeing that all response headers are available.

Property

readyState

The status of the HTTP request. When an XMLHttpRequest is initially created, the value of this property starts at 0 and increases until a complete HTTP response is received.

Each of the 5 states has an associated informal name. The following table lists the states, names, and meanings:

Status Name Description
0 Uninitialized Initialization state. The XMLHttpRequest object has been created or reset by the abort() method.
1 Open The open() method has been called, but the send() method has not been called. The request has not yet been sent.
2 Sent The Send() method has been called, the HTTP request has been sent to the web server. No response has been received.
3 Receiving All response headers have been received. The response body is beginning to be received but not yet complete.
4 Loaded The HTTP response has been completely received.

The value of readyState will not decrease unless the abort() or open() method is called during the processing of a request. Each time the value of this property increases, the onreadystatechange event handler is triggered.

responseText

The response body received so far by the server (excluding headers), or an empty string if no data has been received yet.

If readyState is less than 3, this property is an empty string. When readyState is 3, this property returns the portion of the response received so far. If readyState is 4, this property stores the complete response body.

If the response includes a header specifying the character encoding for the response body, use that encoding. Otherwise, assume Unicode UTF-8 is used.

responseXML

The response to the request is parsed as XML and served as Document objectreturns.

status

returned by the server HTTP status code, such as 200 for success and 404 for "Not Found" error. Reading this property when the readyState is less than 3 will cause an exception.

statusText

This property specifies the HTTP status code of the request by name rather than by number. That is, when the status is 200, it is "OK", and when the status is 404, it is "Not Found". Like the status attribute, reading this property when the readyState is less than 3 will cause an exception.

Event handler

onreadystatechange

An event handler function called each time the readyState attribute changes. It may also be called multiple times when the readyState is 3.

Method

abort()

Cancel the current response, close the connection, and end any pending network activities.

This method resets the XMLHttpRequest object to a readyState of 0 and cancels all pending network activities. For example, if a request takes too long and the response is no longer necessary, this method can be called.

getAllResponseHeaders()

Return the HTTP response headers as uninterpreted strings.

If the readyState is less than 3, this method returns null. Otherwise, it returns all HTTP response headers sent by the server. Headers are returned as a single string, one header per line. Each line is separated by a newline character "\r\n".

getResponseHeader()

Return the value of the specified HTTP response header. The parameter is the name of the HTTP response header to be returned. You can specify the header name in any case, and the comparison with the response header is case-insensitive.

The return value of this method is the value of the specified HTTP response header. If this header is not received or the readyState is less than 3, it is an empty string. If multiple headers with the specified name are received, the values of these headers are concatenated and returned, separated by commas and spaces.

open()

Initialize HTTP request parameters, such as URL and HTTP method, but do not send the request.

send()

Send an HTTP request using the parameters passed to the open() method, as well as the optional request body passed to this method.

setRequestHeader()

Set or add an HTTP request to an open but not yet sent request.

XMLHttpRequest.open()

Initialize HTTP request parameters

Syntax

open(method, url, async, username, password)

The method parameter is the HTTP method used for the request. The values include GET, POST, and HEAD.

The url parameter is the subject of the request. Most browsers implement a same-origin security policy and require that this URL have the same hostname and port as the text containing the script.

The async parameter indicates that the request should be executed asynchronously. If this parameter is false, the request is synchronous, and subsequent calls to send() will be blocked until the complete response is received. If this parameter is true or omitted, the request is asynchronous, and typically requires an onreadystatechange event handler.

The username and password parameters are optional and provide credentials for authorization as required by the url. If specified, they will override any credentials specified by the url itself.

Description

This method initializes the request parameters for use with the send() method later. It sets the readyState to 1, deletes all previously specified request headers and all previously received response headers, and sets the responseText, responseXML, status, and statusText parameters to their default values. It is safe to call this method when the readyState is 0 (when the XMLHttpRequest object is just created or the abort() method is called) and when the readyState is 4 (when the response has been received). When called for any other state, the behavior of the open() method is as specified.

The open() method does not have any other behavior besides saving the request parameters for use with the send() method and resetting the XMLHttpRequest object for reuse. It is particularly important to note that when this method is called, the implementation typically does not open a network connection to the web server.

XMLHttpRequest.send()

Send an HTTP request

Syntax

send(body)

If the HTTP method specified by calling open() is POST or PUT, the body parameter specifies the request body as a string or Document objectIf the request body is not required, this parameter is null. For any other method, this parameter is unavailable and should be null (some implementations do not allow the parameter to be omitted).

Description

This method causes an HTTP request to be sent. If open() has not been called previously, or more specifically, if the readyState is not 1, send() throws an exception. Otherwise, it sends an HTTP request consisting of the following parts:

  • The HTTP method, URL, and credentials (if any) specified by the previous call to open().
  • The request header specified by the previous call to setRequestHeader() (if any).
  • Passed to this method body Parameters.

Once the request is issued, send() sets the readyState to 2 and triggers the onreadystatechange event handler.

If the previously called open() parameter async is false, this method will block and not return until the readyState is 4 and the server's response has been completely received. Otherwise, if the async parameter is true, or if the parameter is omitted, send() returns immediately, and as introduced later, the server response will be processed in a background thread.

If the server response includes an HTTP redirect, the send() method or background thread automatically follows the redirect. When all HTTP response headers have been received, the send() or background thread sets the readyState to 3 and triggers the onreadystatechange event handler. If the response is long, the send() or background thread may trigger the onreadystatechange event handler in state 3: this can be used as a download progress indicator. Finally, when the response is complete, the send() or background thread sets the readyState to 4 and triggers the event handler for the last time.

XMLHttpRequest.setRequestHeader()

Syntax

setRequestHeader(name, value)

The name parameter is the name of the header to be set. This parameter should not include whitespace, colons, or newline characters.

The value parameter is the value of the header. This parameter should not include newline characters.

Description

The setRequestHeader() method specifies a header for an HTTP request, which should be included in the request sent by subsequent send() calls. This method can only be called when the readyState is 1, for example, after calling open() but before calling send().

If a header with the specified name has already been set, the new value of this header is: the previously specified value, plus a comma, whitespace, and the value specified by this call.

If the open() call specifies credentials, XMLHttpRequest automatically sends an appropriate Authorization request header. However, you can use setRequestHeader() to add this header. Similarly, if the web server has already saved and transmitted a cookie associated with the URL passed to open(), the appropriate Cookie or Cookie2 header is also automatically included in the request. These cookies can be added to the header by calling setRequestHeader(). XMLHttpRequest can also provide a default value for the User-Agent header. If it does, any value you specify for this header will be added to the end of this default value.

Some request headers are set automatically by XMLHttpRequest instead of being set by this method to comply with the HTTP protocol. This includes the following headers related to proxies:

  • Host
  • Connection
  • Keep-Alive
  • Accept-charset
  • Accept-Encoding
  • If-Modified-Since
  • If-None-Match
  • If-Range
  • Range