XmlHttpRequest object of ajax

Source: Internet
Author: User

The XMLHttpRequest object is the core of ajax. It has many attributes, methods, and events to facilitate Script Processing and control http request responses. If you have been using native javascript for development, you need to know about this object in the process of using ajax. If you use an encapsulation framework such as jquery, so much understanding of it can also deepen your understanding of ajax. The following describes some of its attributes: 1. readState attribute: After an xmlHttpRequest object is created, this attribute tells you the status of the core object after it is created, whether it can be accessed or processed, and whether readState can be equal to 0, 1, 2, 3, 4; indicates, 0: The created object, but not initialized (such as: int I;), 1: The open () method of the called object, ready to send the request, 2: The send () method has been called to request, but no response has been received. 3: Response in progress; 4: Response completed; httpResponse response received. 2. responseText property: contains the text content received by the client from the http response. When readState is 0, 1, or 2, this attribute is an empty string. When it is 3, it is a part of the received string. When it is 4, this attribute is complete response information. 3. responseXml attribute: only when readyState is 4 and the Content-Type in the response header is set to XML (text/xml or application/xml, the value of this attribute is an xml document. Otherwise, it is null. If the xml format returned is poor or the response is not completed, the attribute value is also null. It is used to describe the attributes of the xmlHttpRequest object after parsing the xml document. 4. status attribute: indicates the http status code. This attribute can be accessed only when readyState is 3 or 4. Otherwise, an exception is thrown when this attribute is obtained. 5. statusText attribute: indicates the text of the http status code. This attribute can be accessed only when readyState is 3 or 4. Otherwise, an exception is thrown when this attribute is obtained. The following is an event description: The onreadystatechange event triggers this event whenever the attribute of readyState changes, which is the most frequently used core event in ajax requests. The following describes some methods: 1. open (): xmlHttpRequest object is initialized by calling the open (method, uri, async, username, password) method. After this method is called, an object that can be sent (send () is returned. Method is a required parameter. It can be GET, POST, PUT, DELETE, and HEAD according to http specifications. uri is used to specify the server address of the xmlHttpRequest object request, which can be a relative or absolute path, the two parameters of username and password can be used in the end. async is used to specify whether the request is asynchronous. The default value is true. If the server needs to verify the access to the user, the two parameters of username and password can be used. 2. the send (): send () method sends a request to parameters in the open () method, that is, it can only be called after open () is called, that is, after readState = 1, send () can be called. Before send () receives the response information, readyState = 2. Once send () receives the response information, readyState = 3, until the final acceptance is completed, readyState = 4. The send method is also expensive. This parameter can contain variable types of data. For large. For most other data types, you should use setRequestHeader () to set Content-Type before calling send. Generally, send (null) is used to display and call this method. If the data type in send (data) is DOMString, the data is edited to UTF-8. If the data type is Document, the data is. the specified xmlEncoding encoding serializes the data. 3. about (): You can pause sending or receiving an httpRequest request, and set the xmlHttpRequest object to the initialization status. 4. setRequestHeader (): Set the request header information. When readyState is set to 1, you can call open (). Otherwise, an exception occurs .. 5. setResponseHeader (): used to retrieve response header information. It can be called only when readyState is 4 or 3. Otherwise, an empty string is obtained. In addition, getAllResponseHeader () is used to obtain information about all httpResponse headers.

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.