The Ajax tool class based on JavaScript encapsulation

Source: Internet
Author: User

The previous paragraph is a piece of work because of the need to write a JavaScript package to give the tool class, technology is limited, Miss Spray, thank you for your support.

1. The following is the JavaScript Ajax tool class.

functioncreatexmlhttprequest () {varreq; if(window. XMLHttpRequest) {//compatible with browsers that are non-IE compatible and IE7req =NewXMLHttpRequest (); }Else if(window. ActiveXObject) {//available in Internet Explorer 5.5 and later versions        Try{req=NewActiveXObject ("Microsoft.XMLHTTP"); } Catch(e) {//available in Internet Explorer 6req =NewActiveXObject ("Msxml2.xmlhttp"); }    }        returnreq;}/*parameter Description: Method: Method of submission (get,post) URL: Requested path param: parameter to pass async: Whether a step type: return value type (Xml,json, default string) fn200: is a function Things to do after a successful request fn404: is a function processing request failed to report 404 error fn500: is a function processing request failed to report 500 error*/functionSendajaxreq (method,url,param,async,type,fn200,fn404,fn500,loading) {varreq =createxmlhttprequest (); //2. Define the processing responseReq.onreadystatechange =function (){        if(Req.readystate = = 4){            if(Req.status = = 200){                if(fn200) {varresult; if(NULL! = Type && ' xml ' = =type.tolowercase ()) {Result=Req.responsexml; }Else if(NULL! = Type && ' json ' = =type.tolowercase ()) {Jsonstr=Req.responsetext; if(document.all) {result= eval (' (' + jsonstr + ') '); }Else{result=Json.parse (JSONSTR); }                    }Else{result=Req.responsetext;                                    } fn200 (Result); }            }Else if(Req.status = = 404){                if(fn404) {fn404 (); }            }Else if(Req.status = = 500){                if(fn500) {fn500 (); }            }        }Else{            if(Loading) {loading ();    }        }    }; if(' get ' = =method.tolowercase ()) {Req.open (method, URL+ (param = =NULL? ‘‘ : ‘?‘ +param), async); Req.send (NULL); }Else if(' post ' = =method.tolowercase ()) {        //1. Define whether the requested address is asynchronous in the way the request is sent;Req.open (method, URL, async); Req.setrequestheader ("Content-type", "application/x-www-form-urlencoded");    Req.send (param); }  }

2. Call Mode:

functionTest () {varurl = "Test?date=" +NewDate (); Sendajaxreq ("Get", url,NULL,true, ' JSON ',function(Result) {//here is the callback method after the request succeeds, can do the processing after the request succeeds, result is the parameter returned by the background},function() {//Here you can jump to a 404 page},function() {//here is the handling of the 500 error},function() {//dealing with other issues});}

The Ajax tool class based on JavaScript encapsulation

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.