CopyCode The Code is as follows: // create an xhr object
VaR xhr;
If (window. XMLHttpRequest ){
Xhr = new XMLHttpRequest ();
}
Else if (window. activexobject ){
Xhr = new activexobject ("msxml2.xmlhttp ");
}
Else {
Throw new error ("Ajax is not supported by this Browser ");
}
Function ready ()
{
Alert ("start ......");
// Process asynchronous requests through events
Xhr. onreadystatechange = function ()
{
If (xhr. readystate = 4)
{
Alert ("ready .");
If (xhr. Status = 200)
{
Alert ("the result returned by the server is successfully obtained .");
// After the request ends, you can obtain the content returned by the server.
Alert (xhr. responsetext );
// Obtain the JSON object returned by the server
VaR Alice = eval ("(" + xhr. responsetext + ")");
Alert (Alice. Name );
}
}
};
// Set Request Parameters
Xhr. Open ("get", "data. JSON ");
Xhr. Send (null );
}
Jquery simply encapsulates the use of xhr objects. By adding common access methods to jquery objects, jquery objects are provided for use. Copy code The Code is as follows: // The main extension is in jquery. Ajax.
Jquery. Extend ({/// #6299
// Default Request Parameters
Ajaxsettings :{
URL: location. href,
Type: "Get ",
Contenttype: "application/X-WWW-form-urlencoded ",
Data: NULL,
Xhr: window. XMLHttpRequest & (window. Location. protocol! = "File:" |! Window. activexobject )?
Function (){
Return new window. XMLHttpRequest ();
}:
Function (){
Try {
Return new window. activexobject ("Microsoft. XMLHTTP ");
} Catch (e ){}
}
},
// Set jquery. ajaxsettings and Request Parameters
Ajaxsetup: function (settings ){
Jquery. Extend (jquery. ajaxsettings, settings );
},
Ajax: function (origsettings) {// actual Ajax Function
VaR S = jquery. Extend (true, {}, jquery. ajaxsettings, origsettings );
// Create an xhr object
Xhr = S. xhr ();
// Callback function
VaR onreadystatechange = xhr. onreadystatechange = function (istimeout ){
If (xhr. readystate === 4 ){
If (xhr. Status = 200 ){
S. Success. Call (origsettings, xhr. responsetext );
}
}
};
// Set Request Parameters
Xhr. Open (S. type, S. url );
// Send the data to send the request
Xhr. Send (S. data );
// Return XMLHttpRequest to allow aborting the request etc.
Return xhr;
},
// Method for sending Ajax requests using get
Get: function (URL, Data, callback, type ){
// Shift arguments if data argument was omited
If (jquery. isfunction (data )){
Type = type | callback;
Callback = data;
Data = NULL;
}
Return jquery. Ajax ({
Type: "Get ",
URL: URL,
Data: data,
Success: callback,
Datatype: Type
});
}
}); // #6922
// Expand the jquery object and add the Load Method
Jquery. FN. Extend (
{
Load: function (URL ){
VaR self = this;
Jquery. Get (URL, function (data ){
Self. Each (function (){
This. innerhtml = data;
}
)
}
)
}
}
)
on the page, you can use the following. copy Code the code is as follows: