Copy codeThe Code is as follows:
<Script>
Var xhr = '';
Function Ajax ()
{
If (window. XMLHttpRequest)
{
Var xhr = new XMLHttpRequest; // modern Browser
} Else
{
Var xhr = new ActiveXObject ('Microsoft. xmlhttp'); // IE
}
}
Ajax (); // 1. Obtain the Ajax object
Xhr. onreadystatechange = function ()
{
If (xhr. readyState = 4 & xhr. status = 200)
{
Var data = xhr. responseText; // 2. determine the status, receive data (in string format), and perform subsequent operations
}
}
// Xhr. open ('request method ', 'request addresses', TRUE); // 3. asynchronous transmission connection
// Xhr. send ([data]); // 4. send the request
Xhr. open ('get', 'demo. php? Name = weichen & sex = 0', TRUE );
Xhr. send ();
/*
Xhr. open ('post', 'demo. php', TRUE );
Xhr. setResponseHeader ('content-type', 'application/x-www-form-urlencoded ');
Xhr. send ('name = weichen & sex = 0 ');
*/
Member attributes and member methods in XHR objects
Copy codeThe Code is as follows:
{
Member attributes:
HTTP status Code (for example, 200 or 404) returned by the status Server)
HTTP status code (OK or not found) returned by the statusText server in string format)
Status code requested by readyState browser (0 to 4)
ResponseText Server Response string format data result
ResponseXML Server Response XML format data result
Onreadystatechange Ajax request event (the event processing function called when readystate changes)
The Mozilla event handler called when an error occurs in the onerror request process
The mozilla event handler called after the onprogress content is loaded
The mozilla event processing function called after the onload document is loaded
Member method:
SetRequestHeader (); set the header information of the current request (add a key/value pair for the message header to be sent to the server)
Open (); create a new request connection (use GET, POST, URL, and other initialization XHR objects)
Send (); send a request (data that may need to be sent)
GetResponseHeader ("server"); returns the value of the specified HTTP header (such as server or last-modified)
GetAllResponseHeaser (); returns all HTTP header strings separated by line breaks
Absort (); terminate the request
}