Detailed description of XMLHttpRequest object

Source: Internet
Author: User

Recently I have learned about Ajax, but I have to mention the most important object XMLHttpRequest. This article focuses on this object to pave the way for learning Ajax.

The main function of the XMLHTTPRequest object is to interact with the server. The difference is that it can send requests to the server and receive data from the server after the page is loaded, to update page data without refreshing a new page.

XMLHttpRequest attributes

Readystate

When an XMLHttpRequest is created for the first time, the value of this attribute gradually increases from 0 to 4. There are five statuses, which also reflect the process of gradually improving the HTTP Request status.

  • 0, indicating that XMLHttpRequest has been created or reset by the abort () method.
  • 1. The open () method has been called, but the request has not been sent.
  • 2. The send () method has been called and the request has been sent, but no response has been received.
  • 3. The response header is received, but the specific content of the response has not been received.
  • 4. The HTTP response has been completed.

This attribute is often used in combination with the status attribute in Ajax to determine the current response status. The value of readystate increases sequentially without interruption,The onreadystatechange event is triggered every time the readystate changes.(If defined), it can be seen from the test that the step-by-step debugging in IE is obvious, but in Chrome browser, the trigger trend is not visible only during Step-by-process debugging.

Responsetext andResponsexml

Responsetext, the text that is used to receive responses from the server, does not contain header data, which is UTF-8 encoded by default. It can also be inferred from the previous attribute that this attribute has data only when readystate is 3 or 4. The difference between responsexml and responsetext is that the XML data returned by responsexml. Status and statustextThe status attribute is the status attribute returned by the server. For example, if the success rate is 200, no Error 404 is found. statustext indicates the same meaning, but uses a string, for example, if it succeeds, it is "OK" and no error is found as "not found ". XMLHttpRequest Method
Abort ()
You can call this method to restore XMLHttpRequest to the uninitialized status when you need to interrupt the connection and response.
GetAllResponseHeaders ()
It returns all HTTP response header information sent by the server, including content-lengh, date, and Uri, which are returned as strings. Each key name and key value are separated by a colon, the keys in each group are separated by carriage return and Line Break (\ r \ n.
GetResponseHeader ()
Returns the value of the specified HTTP Response Header. If multiple headers with the specified name are received, the value of this header is connected and returned, use commas (,) and spaces to separate the values of each header.
Open ()
The open (method, URL, async, username; password) method can have up to five parameters: Call method "get" or "Post", server Address URL, and asynchronous transmission; user name and password. The last two parameters are used for authentication. This method is used to develop methods, addresses, and request information for interacting with the server.
Send (content)
Send an HTTP request. If it is asynchronous, this method will return immediately. If you do not need to send the content, the content is null. You can also send the Dom or string.
SetRequestHeader (header, value)
Set the value of the header in the HTTP request to value, This method is used after open (). XMLHttpRequest event
XMLHttpRequest also has an event handle onreadystatechange, which is assigned to this handle as a function, that is, the callback function. When the server returns data and meets the conditions, it can be used to update page data.

How can I use it if I have said so much? I will introduce it in the next blog.

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.