基於JavaScript封裝的Ajax工具類

來源:互聯網
上載者:User

標籤:microsoft   java   跳轉   new   text   explore   try   activex   send   

前段是件由於工作需要無奈編寫了一個給予JavaScript封裝的工具類,技術有限,誤噴,感謝大家的支援。

1、以下是JavaScript 的 Ajax 工具類。

function createXMLHttpRequest(){    var req;    if(window.XMLHttpRequest){        //相容非IE  並且相容 IE7以上的瀏覽器        req = new XMLHttpRequest();    }else if(window.ActiveXObject){        //在 Internet Explorer 5.5 及其後版本中可用        try {            req = new ActiveXObject("Microsoft.XMLHTTP");        } catch (e) {            //在 Internet Explorer 6 中可用            req = new ActiveXObject("Msxml2.XMLHTTP");        }    }        return req;}/*參數介紹:method:提交方式(get,post)url:請求的路徑param:需要傳遞的參數async:是否一步type:傳回值類型(xml,json,預設字串)fn200:是一個function 處理請求成功後的的事情fn404:是一個function 處理請求失敗報404錯誤fn500:是一個function 處理請求失敗報500錯誤*/function sendAjaxReq(method,url,param,async,type,fn200,fn404,fn500,loading){    var req = createXMLHttpRequest();    //2.定義處理響應    req.onreadystatechange = function (){        if(req.readyState == 4){            if(req.status == 200){                if(fn200){                    var result;                    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.定義發送請求  請求的方式   請求的地址    是否非同步;        req.open(method, url, async);        req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");        req.send(param);    }  }

 

2.調用方式:

function test(){     var url = "test?date=" + new Date();     sendAjaxReq("get",url,null,true,‘json‘,function(result){         //此處是請求成功後回調方法,可做請求成功後的處理,result是後台返回的參數     },function (){     //此處可以跳轉一個404頁面   },function (){     //此處是處理500錯誤   },function (){     //處理其他問題   });}

 

基於JavaScript封裝的Ajax工具類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.