Ajax reconstruction steps and Ajax reconstruction steps

Source: Internet
Author: User

Ajax reconstruction steps and Ajax reconstruction steps

Ajax Refactoring can be roughly divided into the following three steps.

1. Create a separate JS file named AjaxRequest. js, and write the code required to refactor Ajax in the file.
The Code is as follows:
Var net = new Object (); // defines a global variable.
// Compile the constructor
Net. AjaxRequest = function (url, onload, onerror, method, params)
{
This. req = null;
This. onload = onload;
This. onerror = (onerror )? Onerror: this. defaultError;
This. loadDate (url, method, params );
}
// Compile the method used to initialize the XMLHttpRequest object, specify the processing function, and finally send the HTTP request
Net. AjaxRequest. prototype. loadDate = function (url, method, params)
{
If (! Method) // set the default REQUEST method TO GET
{
Method = "GET ";
}
If (window. XMLHttpRequest)
{// Non-IE browser
This. req = newXMLHttpRequest (); // create an XMLHttpRequest object
}
Elseif (window. ActiveXObject)
{// IE browser
Try
{
This. req = new ActiveXObject ("Microsoft. XMLHTTP"); // create an XMLHttpRequest object
}
Catch (e)
{
Try
{
This. req = new ActiveXObject ("Msxml2.XMLHTTP"); // create an XMLHttpRequest object
}
Catch (e)
{
}
}
}
If (this. req)
{
Try
{
Varloader = this;
This. req. onreadystatechange = function ()
{
Net. AjaxRequest. onReadyState. call (loader );
}
This. req. open (method, url, true); // create a call to the server
If (method = "POST ")
{// If the submission method is POST
This. req. setRequestHeader ("Content-Type", "application/x-www-form-urlencoded"); // you can specify the request Content Type.
This. req. setRequestHeader ("x-requested-with", "ajax"); // set the request sender
}
This. req. send (params); // send the request
}
Catch (err)
{
This. onerror. call (this); // call the error handling function
}
}
}
 
// Refactor the callback function
Net. AjaxRequest. onReadyState = function ()
{
Var req = this. req;
Var ready = req. readyState; // get the Request status
If (ready = 4)
{// Request completed
If (req. status = 200)
{// Request successful
This. onload. call (this );
}
Else
{
This. onerror. call (this); // call the error handling function
}
}
}
// Refactor the default error handling function
Net. AjaxRequest. prototype. defaultError = function ()
{
Alert ("error data \ n callback status:" + this. req. readyState + "\ n status:" + this. req. status );
}
2. Apply the following statements on the page where Ajax is to be applied, including the JS File Created in step 1.
<Script language = "javascript" src = "AjaxRequest. js"> </script>
 
3. Compile the error handling method on the page where Ajax is applied, and instantiate the Ajax object method and callback function.
The Code is as follows:
<Script language = "javascript">
***************** *********************/
Function onerror ()
{
Alert ("Your operation is incorrect !");
}
/***************** Method for instantiating an Ajax object **************** *************/
Function getInfo ()
{
Var loader = newnet. AjaxRequest ("getInfo. jsp? Nocache = "+ new Date (). getTime (), deal_getInfo, onerror," GET ");
}
/************************ Callback function ************* *************************/
Function deal_getInfo ()
{
Document. getElementById ("showInfo"). innerHTML = this. req. responseText;
}
</Script>

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.