Getting Started with Ajax---properties and methods of XMLHttpRequest objects

Source: Internet
Author: User
Tags http authentication

Because it is new to Ajax, the XMLHttpRequest object is unheard of. Before you start, learn about its properties and methods to make a difference. This article focuses on the properties and methods of XMLHttpRequest.

XMLHttpRequest object properties and events

Property                                               description

readyState                                            Indicates the state of the XMLHttpRequest object [1]

responseText                                          contains the corresponding text content of the HTTP received by the client [2]

responseXML                                          The DOM object corresponding to the XML content of the server response [3]

Status                                                    server returns http status code [4]

status                                                    Text server returns text information of status code [5]

Event                                                     description

Onreadystatechange                                This event is fired when the readyState property changes to trigger the callback function.


Status                                 Name                                 Description

0                                   Uninitialized                           initialization state. The XMLHttpRequest object has been created 

                                                                                 or has been reset by the abort() method.

1                                       Open                                 open() method has been called, but the send() method is not called. 

                                                                                 The request has not been sent.

2                                       Send                                  Send() method has been called and an HTTP request has been sent

                                                                                 to the web server. No response was received.

3                                    Receiving                               All response headers have been received. The response body

                                                                                  begins  to receive but is not completed.

4                                      Loaded                                HTTP response has been completely received.

[2]:When readystate=4, ResponseText contains the complete response information.

When readystate=3, ResponseText contains incomplete response information.

When Readystate<3, the ResponseText is an empty string.

[3]: When readystate=4, and the Content-type MIME type of the response head is XML (Text/xml or Application/xml), the attribute has a value and is parsed into an XML document. Other cases are null, including the return of an XML document that is bad or does not complete the response.

[4]: If 200 indicates success, and 404 indicates "NotFound" error. Reading this property when ReadyState is less than 3 results in an exception.

[5]: When the state is 200 it is "OK" and when the state is 404 it is "not Found". As with the Status property, reading this property when ReadyState is less than 3 results in an exception.

The method of the XMLHttprequest object

Method method 

Description: Develop HTTP method with server interaction, URL address and other request information.

Open (Method,url, async, username, password) for initialization

Return value: Gets an object that contains the Send () method

Method: Must be. Used to specify HTTP request methods that support all HTTP methods, such as Get,post, as specified

URI: The address of the requested server, which is automatically parsed into an absolute address.

Async: The request is asynchronous, true means that you are asynchronous, False indicates synchronization, default is True.

Username,password: You can specify the username and password, respectively, and provide the user name and password required by the HTTP authentication mechanism.

After calling open, the readystate state is 1. 

Method 2.send (content) 

Description: Makes a request to the server, and its contents can be DOM objects, input streams, or strings.

After you call the Open method, you can call the Send () method to send the request.

When async=true in Open, the Send () method returns immediately after the call, otherwise it is interrupted until the request returns. 

Method 3.abort ()

The method can suspend a HttpRequest request or HttpResponse reception, and set the XMLHttpRequest state to initialize. 

Method 4.set RequestHeader (Header,value)

This method is used to set the header information for the request.          

This method needs to be invoked after the open method. 

Method 5.getResponseHeader () 

Description: Returns all response header information containing HTTP, where the response header includes content such as Content-length,date,uri.

When Readystate>2, this method retrieves the header information for the response. Otherwise, an empty string is returned.

The getAllResponseHeaders () method returns all HttpResponse header information.

 knowledge of the XMLHttpRequest object, the focus is on how to use the

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: 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.