Home > Backend Development > XML/RSS Tutorial > ajax xmlHttpRequest object

ajax xmlHttpRequest object

黄舟
Release: 2017-02-23 14:56:49
Original
1389 people have browsed it

Please indicate the source when reprinting: ajax’s xmlHttpRequest object
The XMLHttpRequest object is the core of ajax. It has many properties, 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 this object very well when using ajax. If you use an encapsulation framework such as jquery, having a better understanding of it will also deepen your understanding of it. Ajax understanding.

The following is an introduction to some of its attributes:

1.readState attribute: When the xmlHttpRequest object is created, this attribute will tell you that the core object is created Finally, what state is it in and whether it can be accessed or processed? readState can be equal to 0, 1, 2, 3, 4; respectively, 0: the object has been created but not initialized (such as: int i;), 1: has Call the open() method of the object and prepare to send the request. 2: The send() method has been called to request, but no response was received. 3: Responding. 4: Completed the response and completed the reception of the httpResponse response.

          2.responseText attribute: Contains the text content of the http response received by the client. When readState is 0, 1 or 2, this attribute is an empty string. When it is 3, it is a partially received string. When it is 4, this attribute is the complete response information.

        3.responseXml attribute: Only when readyState=4 and the Content-Type type of the response header is specified as XML (text/xml or application/xml), the value of this attribute is an xml document. Otherwise, it is null. If the returned xml is in poor format or the response is not completed, the value of this attribute will also be null. It is used to describe the properties of the xmlHttpRequest object after parsing the xml document.

        4.status attribute: marked with http status code. This property can only be accessed when readyState=3 or 4, otherwise an exception will be thrown when obtaining this property.

          5.statusText attribute: The text marked with the http status code. This property can only be accessed when readyState=3 or 4, otherwise an exception will be thrown when obtaining this property.

The following is an introduction to one of its events: onreadystatechange event

Whenever the properties of readyState change, this event will be triggered, which is ajax The most frequently used core events in requests.

The following is an introduction to some methods:

1.open(): The xmlHttpRequest object is executed by calling the open(method, uri, async, username, password) method. Initialization work. After calling this method, an object that can be sent (send() method) is returned. method is a required parameter. According to the http specification, it can be GET, POST, PUT, DELETE and HEAD; uri is used to specify the server address requested by the xmlHttpRequest object, which can be a relative path or an absolute path, and will eventually be resolved into an absolute path; async Used to specify whether it is asynchronous, the default is true; if the server needs to verify the access user, then the two parameters of username and password can be used.

        2.send(): The send() method requests to send the parameters in the open() method, that is, it can only be called after the open() call, that is, send can be called after readState=1 (), 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 takes an optional parameter, which can contain variable type data. For large. For most other data types, you should use setRequestHeader() to set the Content-Type before calling send(). Generally, send(null) is used to explicitly call this method. If the data type in send(data) is DOMString, the data is edited to utf-8. If it is Document, the encoding string specified by

data.xmlEncoding Rowize this data.

3.about(): You can pause the sending or receiving of an httpRequest request, and set the xmlHttpRequest object to the initialized state.

4.setRequestHeader(): Set the header information of the request. When readyState=1, you can call it after calling open(), otherwise you will get an exception.

.     5.setResponseHeader(): used to retrieve response header information. It can only be called when readyState=4 or 3, otherwise an empty string will be obtained. In addition, getAllResponseHeader() is used to obtain all httpResponse header information.

# and above are the contents of the XMLHTTPRequest object of Ajax. For more related content, please pay attention to PHP Chinese (m.sbmmt.com)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template