Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

XHR creates the object


May 08, 2021 AJAX


Table of contents


AJAX - Create an XMLHttpRequest object


XMLHttpRequest is the foundation of AJAX.

The XMLHttpRequest term is abbreviated as Chinese and can be interpreted as an extensable hyperbase transfer request.

The XMLHttpRequest object enables a partial update of the page without submitting the entire page to the server.

XMLHttpRequest objects are used for asynchronous communication between the client and the server.

It does the following:

  1. Send data from the client in the background
  2. Receive data from the server
  3. Update the page without reloading.

XMLHttpRequest object


All modern browsers support XMLHttpRequest objects (IE5 and IE6 use ActiveXObject).

XMLHttpRequest is used to exchange data with the server in the background. This means that a portion of the page can be updated without reloading the entire page.


Create an XMLHttpRequest object


All modern browsers (IE7 plus, Firefox, Chrome, Safari, and Opera) have built-in XMLHttpRequest objects.

Create the syntax of the XMLHttpRequest object:

variable=new XMLHttpRequest();

Older versions of Internet Explorer (IE5 and IE6) use ActiveX objects:

variable=new ActiveXObject("Microsoft.XMLHTTP");

To handle all modern browsers, including IE5 and IE6, check that the browser supports XMLHttpRequest objects. I f supported, the XMLHttpRequest object is created. If not, create ActiveXObject:

var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

Try it out . . .

Now that you've learned how to create an XMLHttpRequest object (which is the basis for learning AJAX), you'll learn how to send server requests in the next chapter.

The properties of the XMLHttpRequest object

Common properties of XMLHttpRequest objects are as follows:

Property Describe
onreadystatechange
Store a function (or function name) that is called whenever the properties of readyState change.
readyState The state of the existing XMLHttpRequest changes from 0 to 4.
0: The request is not initialized
1: The server connection has been established
2: The request has been received
3: Request processing
4: The request is complete and the response is ready
reponseText The response is returned as text.
responseXML Returns the response in XML format
status
Return the status as a number (for example, "Not Found" is 404, "OK" is 200)
statusText
Return a state as a string (for example, "Not Found" or "OK")

XMLHttpRequest object method

The important methods for XMLHttpRequest objects are as follows:

Method Describe
abort()
Cancel the current request.
getAllResponseHeaders()
Returns the complete HTTP header set as a string.
getResponseHeader( headerName )
Returns the value of the specified HTTP header.
void open(method,URL) Open the request that specifies the method and URL to be obtained or handed over.
void open(method,URL,async) Same as above, but specified asynchronous or not.
void open(method,URL, async userName password
Same as above, but specifying a username and password.
void send(content) Send a get request.
setRequestHeader( label value
Add the label/value pair to the HTTP header to send.

Related articles


Ajax XMLHttpRequest