Steps for Ajax Refactoring

Source: Internet
Author: User

Tag: Log time () callback load Ted loader his instantiation initialization

Ajax refactoring can be broadly divided into the following 33 steps.

Create a separate JS file with the name Ajaxrequest.js, and write the code needed to refactor Ajax in the file
The specific code is as follows:
var net = new Object (); To define a global variable
Writing constructors
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);
}
Writes the method used to initialize the XMLHttpRequest object and specify the handler function, and finally sends 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 a XMLHttpRequest Object
}
ElseIf (window. ActiveXObject)
{//IE browser
Try
{
this.req= new ActiveXObject ("Microsoft.XMLHTTP"); Create a XMLHttpRequest Object
}
catch (E)
{
Try
{
This.req = new ActiveXObject ("Msxml2.xmlhttp"); Create a 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); Make a call to the server
if (method== "POST")
{//If the submission method is post
This.req.setRequestHeader ("Content-type", "application/x-www-form-urlencoded"); Set the content type of the request
This.req.setRequestHeader ("X-requested-with", "Ajax"); Set the issuer of the request
}
This.req.send (params); Send Request
}
catch (Err)
{
This.onerror.call (this); Calling the error handling function
}
}
}

Refactoring callback functions
Net. Ajaxrequest.onreadystate = function ()
{
var req =this.req;
var ready =req.readystate; GET Request Status
if (ready = = 4)
{//Request completed
if (req.status== 200)
{//Request succeeded
This.onload.call (this);
}
Else
{
This.onerror.call (this); Calling the error handling function
}
}
}
Refactoring default error-handling functions
Net. AjaxRequest.prototype.defaultError = function ()
{
Alert ("error data \ n \ n Callback state:" + this.req.readyState + "\ n State:" + this.req.status);
}
Second, apply the following statement to the page where you want to apply Ajax, including the JS file created in step one
<script language = "javascript" src = "ajaxrequest.js" > </script>

Three ways to write error handling in AJAX-enabled pages, instantiating Ajax object methods and callback functions
The specific code is as follows:
<script language = "JavaScript" >
/****************** method of error handling **************************************/
function OnError ()
{
Alert ("Your operation is wrong!") ”);
}
/****************** methods for instantiating Ajax objects *********************** ******/
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>

Steps for Ajax Refactoring

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.