Simulation code of jquery implementation principle-5 Ajax

Source: Internet
Author: User

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:















Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.