using Ajax to define a XMLHttpRequest variable first, in order to determine compatibility with IE6 and the following versions, you need to
var xmlhr;
if (window. XMLHttpRequest) {
XMLHR = new XMLHttpRequest ();
}else{
XMLHR = new ActiveXObject ("Microsoft.XMLHTTP");
}
Common Properties and methods for XMLHttpRequest objects are:
three common properties of the XMLHttpRequest object:
1. onreadystatechange Properties
the onReadyStateChange property has functions that handle server responses.
The following code defines an empty function that can be set at the same time as the onReadyStateChange property:
xmlhr.onreadystatechange=function ()
{
//We need to write some code here
}
2.readyState Properties
The ReadyState property holds state information about the server response. The onreadystatechange function is executed whenever readyState changes.
possible values for the ReadyState property:
Status Description
0 The request was not initialized (before calling open ())
1 request has been made (before calling Send ())
2 The request has been sent (here you can usually get the content header from the response)
3 in Request processing (usually some of the data in the response is available, but the server has not yet completed the response)
4 request completed (Can access server response and use it)
we want to add an if statement to this onreadystatechange function to test whether our response is complete (which means we can get the data):
xmlhr.onreadystatechange=function ()
{
if (xmlhttp.readystate==4)
{
//Get data from the server's response
}
}
3.responsetext Properties
the data returned by the server can be retrieved through the ResponseText property.
in our code, we will set the value of the time text box to equal to ResponseText:
xmlhr.onreadystatechange=function ()
{
if (xmlhr.readystate==4)
{
Document.myform.time.value=xmlhttp.responsetext;
}
}
Methods of Xmlhttprequst
1.open () method
Open () has three parameters. The first parameter defines the method used to send the request, the second parameter specifies the URL of the server-side script, and the third parameter specifies that the request should be processed asynchronously.
Xmlhr.open ("GET", "test.php", true);
2.send () method
the Send () method will send the request to the server. If we assume that the HTML file and the PHP file are in the same directory, then the code is:
xmlhr.send (null);
XMLHttpRequest Ajax uses the general steps
Open ("GET" or "POST") setRequestHeader ("Content-type", "application/x-www-form-encoded") Send Onreadystat Echange
The Ajax use of JavaScript