The content of this article is what is XMLHttpRequest in Ajax? A brief introduction to XMLHttpRequest to let everyone know some XMLHttpRequest methods and attributes that must be familiar with and mastered. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
XMLHttpRequest object can be used to exchange data with the server in the background. Ajax obtains background data by using the XMLHttpRequest object in the browser. Therefore, XMLHttpRequest is very important for Ajax. It can be said that the XMLHttpRequest object is AJAX The key to technology. [Related video recommendations: Ajax Video Tutorial]
Let’s learn about the relevant knowledge of XMLHttpRequest!
1. What is XMLHttpRequest?
XMLHttpRequest (XHR) is an API that can be used by JavaScript, JScript, VBScript and other web browser scripting languages to transfer XML data to and from a web server using HTTP. The server manipulates XML data to establish an independent connection channel between the client and server sides of the web page.
The data returned from the XMLHttpRequest call is usually provided by the backend database. In addition to XML, XMLHttpRequest can be used to obtain data in other formats, such as JSON or even plain text.
Next let's take a look at some XMLHttpRequest methods and properties that must be familiar with.
2. XMLHttpRequest method
##1. abort(): Cancel the current request.
2. getAllResponseHeaders(): Returns the complete set of HTTP headers in string form.
3. getResponseHeader( headerName ): Returns the value of the specified HTTP header.
4. open( method, URL) open( method, URL, async )
open( method, URL, async, userName )
open( method, URL, async, userName, password )
Method parameters can have the value "GET", "POST" or "HEAD". Other HTTP methods such as "PUT" and "DELETE" (mainly used in REST applications) are also possible.
The "async" parameter specifies whether the request should be processed asynchronously. "true" means that script processing continues after the send() method without waiting for a response, "false" means that the script waits for a response before continuing script processing.
3. XMLHttpRequest attributes
An event handler Procedure for triggering events for each state change.
2. readyState:The readyState attribute defines the current state of the XMLHttpRequest object.
The following table provides a list of possible values for the readyState attribute
readyState = 0: Indicates that after the XMLHttpRequest object is created, but before the open() method is called .
readyState = 1: Indicates after calling the open() method, but before calling send().
readyState = 2: Indicates after calling send().
readyState = 3: Indicates that after the browser establishes communication with the server, but before the server completes the response.
readyState = 4: Indicates that the response data has been fully received from the server after the request is completed.
3. responseTextReturns the response in string form.
4. responseXMLReturns the response in XML format; this property returns an XML document object, which can be inspected and parsed using W3C DOM node tree methods and properties.
5, statusReturn the status as a number (for example, 404 means "not found", 200 means "OK").
Returns the status as a string (for example, "not found" or "OK").
Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.
The above is the detailed content of What is XMLHttpRequest in Ajax? A brief introduction to XMLHttpRequest. For more information, please follow other related articles on the PHP Chinese website!