Function getajax (){
// Create an XMLHttpRequest instance
VaR xhr = getxmlhttprequest ();
// Load the Open Method
Xhr. Open ("get", "index. jsp? Username = guoqianfang ", true );
/*
There are five parameters in the Open Method: method/URL/async/user/password. Three parameters are commonly used: method, URL, and async,
Method: Get and post. What is the difference between them? And & Symbol link cannot exceed 2 K; When a POST request
Adding parameters to a URL is safe, but can also be loaded to a URL and get the loaded parameters.
URL: It can be relative or absolute.
Async: true (asynchronous), false (synchronous, generally not used). The default value is true.
*/
Xhr. Send ();
/*
When a GET request is sent, you do not need to set xhr. setRequestHeader (header, value). The parameters of the send method can be written as null without writing anything,
If you want to write parameters in the send method, you must set xhr. setRequestHeader (header, value), which is a post Method Request by default.
The parameters in the send method cannot be obtained.
Xhr. setRequestHeader (header, value) must be set for post requests );
*/
// Write the call Function
Xhr. onreadystatechange = function (){
// Determine whether the call is complete
If (xhr. readystate = 4 ){
// Determine whether the server is successfully processed
If (xhr. Status = 200 ){
$ ("Content"). innerhtml = xhr. responsetext;
}
}
}
}
Function postajax (){
VaR xhr = getxmlhttprequest ();
Xhr. Open ("Post", "index. jsp", true );
Xhr. setRequestHeader ("Content-Type", "application/X-WWW-form-urlencoded ");
Xhr. Send ("username = guoqianfang ");
Xhr. onreadystatechange = function (){
// Determine whether the call is complete
If (xhr. readystate = 4 ){
// Determine whether the server is successfully processed
If (xhr. Status = 200 ){
$ ("Content"). innerhtml = xhr. responsetext;
}
}
}
}
Function $ (ID ){
Return document. getelementbyid (ID );
}
Function getxmlhttprequest (){
VaR xhr;
Try {
// IE browser
Xhr = new activexobject ("micorsoft. XMLHTTP ");
} Catch (error ){
Try {
// Firefox/operabrowser
Xhr = new XMLHttpRequest ();
} Catch (e ){
Return xhr;
}
}
Return xhr;
}
This is the simplest and most basic knowledge point of Ajax!