The foundation of the XMLHttpRequest object--ajax.

Source: Internet
Author: User
Tags http redirect
The XMLHttpRequest object is the basis of Ajax and is also used as the underlying base in jquery. XMLHttpRequest Objects

The XMLHttpRequest object provides full access to the HTTP protocol, including the ability to make POST and head requests and normal get requests. XMLHttpRequest can return the response of a 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 working with XML documents: It can receive any form of text document.

The XMLHttpRequest object is a key feature of the WEB application architecture called AJAX. Browser Support

XMLHttpRequest has been well supported by all modern browsers. A unique browser dependency involves the creation of a XMLHttpRequest object. In IE 5 and IE 6, you must use the IE-specific ActiveXObject () constructor. Standardization of the Consortium

The XMLHttpRequest object has not yet been standardized, but the consortium has begun to standardize its work, and this manual is based on a standardized work draft.

The current XMLHttpRequest implementations are quite consistent. But it's a slight difference from the standard. For example, an implementation might return NULL, the standard requirement is an empty string, or the implementation might set readyState to 3 without ensuring that all response headers are available. Property readyState

The status of the HTTP request. When a XMLHttpRequest is first created, the value of this property starts at 0 until the full HTTP response is received, and the value increases to 4.

Each of the 5 states has an associated informal name, and the following table lists the status, name, and meaning:

State name Description
0 Uninitialized Initialization state. The XMLHttpRequest object has been created or has been reset by the Abort () method.
1 Open The open () method was invoked, but the Send () method was not invoked. The request has not been sent yet.
2 Sent The Send () method has been invoked and the HTTP request has been sent to the WEB server. The response was not received.
3 Receiving All response headers have been received. The response body began to receive but did not complete.
4 Loaded The HTTP response has been fully received.

The value of the readyState is not decremented unless the abort () or open () method is invoked when a request is in process. Each time the value of this property is incremented, the onReadyStateChange event handle is triggered. ResponseText

The response body received so far for the server (excluding the head), or if the data has not yet been received, is an empty string.

If ReadyState is less than 3, this property is an empty string. When ReadyState is 3, this property returns the part of the response that is currently received. If ReadyState is 4, this property holds the complete response body.

If the response contains a header that specifies a character encoding for the body of the response, the encoding is used. Otherwise, assume the use of Unicode UTF-8. Responsexml

The response to the request, parsed as XML and returned as a Document object. Status

The HTTP status code returned by the server, such as 200, indicates success, while 404 indicates a "not Found" error. Reading this property when ReadyState is less than 3 results in an exception. StatusText

This property specifies the requested HTTP status code with a name instead of a number. That is, when the state is 200 it is "OK" and it is "not Found" when the state is 404. As with the Status property, reading this property when ReadyState is less than 3 results in an exception. Event Handle onreadystatechange

The event handle function that is invoked each time the ReadyState attribute changes. When ReadyState is 3 o'clock, it may also be called multiple times. Method abort ()

Cancels the current response, closes the connection, and ends any pending network activity.

This method resets the XMLHttpRequest object to the state of ReadyState 0 and cancels all pending network activity. For example, this method can be invoked if the request takes too long and the response is no longer necessary. getallresponseheaders ()

Returns the HTTP response header as an unresolved string.

If ReadyState is less than 3, this method returns NULL. Otherwise, it returns the head of all HTTP responses sent by the server. The head is returned as a single string, one head at a row. Each line is separated by a newline character, "\ r \ n". getResponseHeader ()

Returns the value of the specified HTTP response header. The parameter is the name of the HTTP response header to return. You can use any case to make this header name, and the comparison of the response head is case-insensitive.

The return value of the method is the value of the specified HTTP response head, or an empty string if the header is not received or if the readyState is less than 3. If more than one header is received with the specified name, the value of the header is concatenated and returned, separating the values of each head with commas and spaces. Open ()

Initializes HTTP request parameters, such as URLs and HTTP methods, but does not send requests. Send ()

Sends an HTTP request, using the arguments passed to the open () method, and the optional request body passed to the method. setRequestHeader ()

Sets or adds an HTTP request to an open but unsent request.

Xmlhttprequest.open ()

Initialize HTTP request parameter syntax

Open (method, URL, async, username, password)

Method parameters are HTTP methods for the request. Values include get, POST, and head.

The URL parameter is the body of the request. Most browsers implement a homologous security policy and require that the URL have the same host name and port as the text containing the script.

The async parameter indicates that the request usage should be performed asynchronously. If this argument is false, the request is synchronized, and subsequent calls to send () will block until the response is fully received. If this argument is TRUE or omitted, the request is asynchronous and usually requires a onreadystatechange event handle.

The username and password parameters are optional and are qualified for the authorization required for the URL. If specified, they overwrite any qualifications specified by the URL itself. Description

This method initializes the request parameters for later use by the Send () method. It sets the readyState to 1, deletes all headers previously specified, and all response headers previously received, 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 (after the XMLHttpRequest object has just been created or after the abort () method call) and when the ReadyState is 4 o'clock (already receiving the response). When invoked against any other state, the behavior of the open () method is specified.

In addition to saving the request parameters used by the Send () method and resetting the XMLHttpRequest object for reuse, the open () method has no other behavior. Be particularly aware that implementations typically do not open a network connection to a WEB server when this method is invoked. xmlhttprequest.send ()

Send an HTTP request syntax

Send (body)

If the HTTP method specified by calling open () is a POST or put,body parameter, the request body is specified as a string or Document object. This parameter is null if the request body is not necessary. For any other method, this parameter is not available and should be null (some implementations do not allow this argument to be omitted). Description

This method causes an HTTP request to be sent. If you have not called open () before, or more specifically, if ReadyState is not 1,send () throws an exception. Otherwise, it sends an HTTP request that consists of the HTTP method, the URL, and the authentication eligibility (if any) that was specified when the open () was previously called. The requested header (if any) that was specified when setRequestHeader () was called earlier. The body parameter passed to this method.

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

If the previously called open () parameter async to False, this method blocks and does not return until ReadyState is 4 and the server's response is fully received. Otherwise, if the async argument is true, or if this argument is omitted, send () returns immediately, and as described later, the server response is processed in a background thread.

If the server response has an HTTP redirect, the Send () method or the background thread automatically obeys the redirection. When all HTTP response headers have been received, send () or background thread sets the readyState to 3 and triggers the onreadystatechange event handle. If the response is longer, send () or background thread may trigger the onReadyStateChange event handle in State 3: This can be used as a download progress indicator. Finally, when the response is complete, send () or the background thread sets the readyState to 4 and the event handle is last triggered. Xmlhttprequest.setrequestheader () Grammar

setRequestHeader (name, value)

The name parameters are the names of the headers you want to set. This parameter should not include white space, colon, or line wrapping.

The value parameter is the values of the head. This parameter should not include line wrapping. Description

The setRequestHeader () method specifies the head of an HTTP request that should be included in a request that is published through subsequent send () calls. This method can only be invoked when the readyState is 1, for example, after the open () is invoked, but before the call to send ().

If the header with the specified name has been specified, the new value for the header is: the previously specified value, plus a comma, white space, and the value specified by the call.

If the open () call specifies authentication, XMLHttpRequest automatically sends an appropriate Authorization request to the head. However, you can use setRequestHeader () to add this head. Similarly, if the WEB server has saved a cookie associated with the URL that is passed to open (), the appropriate cookie or COOKIE2 header is automatically included in the request. You can add these cookies to the head by calling setRequestHeader (). XMLHttpRequest can also provide a default value for User-agent headers. If it does, any value you specify for the head will be added to the default value.

Some request headers are automatically set by XMLHttpRequest rather than set by this method to conform to the HTTP protocol. This includes the following and agent-related headers: Host Connection keep-alive accept-charset accept-encoding if-modified-since if-none-match Range

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.