Home  >  Article  >  Web Front-end  >  What is XMLHttpRequest in Ajax? A brief introduction to XMLHttpRequest

What is XMLHttpRequest in Ajax? A brief introduction to XMLHttpRequest

青灯夜游
青灯夜游Original
2018-11-08 10:55:523891browse

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 )

Specify the request method, URL and other optional attributes.

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.

5. send(content): Send a request.

6. setRequestHeader(label, value): Add the label/value pair to the HTTP header to be sent.

3. XMLHttpRequest attributes

##1. onreadystatechange:

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

What is XMLHttpRequest in Ajax? A brief introduction to XMLHttpRequestreadyState = 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. responseText

Returns the response in string form.

4. responseXML

Returns 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, status

Return the status as a number (for example, 404 means "not found", 200 means "OK").


6, statusText


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!

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