Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the use of xmlHttpRequest objects in ajax

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "what is the use of xmlHttpRequest objects in ajax", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "what is the use of xmlHttpRequest objects in ajax" this article.

XmlHttpRequest object of ajax

The XMLHttpRequest object is the core of ajax. It has many properties, methods, and events to facilitate script processing and control of http request responses. If you have been using native javascript to do development, then in the process of using ajax, you need to know a lot about this object, if you use a wrapper framework such as jquery, then have a lot of understanding of it, but also deepen your understanding of ajax.

Here is an introduction to some of its properties:

1.readState attribute: when the xmlHttpRequest object is created, this property will tell you what state the core object is in after it is created, and whether it can be accessed or processed. ReadState can be equal to 0, 1, 2, 2, 3, 4; respectively, 0: the object has been created, but not initialized (for example: int I ), 1: the object's open () method has been called, ready to send the request, 2: the send () method has been called to request, but there is no response, 3: responding, 4: completing the response, completing the receipt of the httpResponse response.

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

3.responseXml attribute: the value of this property is a xml document only if readyState=4 and the type of Content-Type of the response header is specified as XML (text/xml or application/xml), otherwise it is null, and it is also null if the xml returned is malformed or the response is not completed. It is used to describe the properties of the xmlHttpRequest object after parsing the xml document.

4.status attribute: marks the http status code. This property can be accessed only if it is readyState=3 or 4, otherwise an exception will be thrown when the property is obtained.

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

Here is an introduction to one of its events: the onreadystatechange event

This event is triggered whenever the property of the readyState changes, which is the most frequently used core event in the ajax request.

The following is an introduction to some methods:

1.open (): the xmlHttpRequest object initializes by calling the open (method,uri,async,username,password) method. After calling this method, it returns an object that can be sent (the send () method). Method is a required parameter. According to http specification, you can specify the server address requested by xmlHttpRequest object for GET,POST,PUT,DELETE and HEAD;uri, which can be relative path or absolute path, and will eventually be resolved to absolute path. Async is used to specify whether to be asynchronous. The default is true;. If the server needs to verify access users, then these two parameters of username and password can be used.

2.send (): the send () method sends the parameter in the open () method to request, that is, it can only be called after open () call, that is, readState=1 can be called after send (), 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 uses also expensive optional parameters, which can contain variable types of data. For big. Most other data types should set Content-Type with setRequestHeader () before calling send (). This method is generally called using the send (null) display. If the data type in send (data) is DOMString, the data is edited as utf-8, and if it is Document, the

The encoding specified by data.xmlEncoding serializes the data.

3.about (): you can pause the sending or receiving of a httpRequest request and set the xmlHttpRequest object to initialization.

4.setRequestHeader (): sets 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, which can only be called when readyState=4 or 3, otherwise you get an empty string. In addition, getAllResponseHeader () is used to get the header information of all httpResponse.

The above is all the content of the article "what is the use of xmlHttpRequest objects in ajax?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report