ajax請求詳解

來源:互聯網
上載者:User

標籤:

//1. 建立一個ajax對象//var xhr=new XMLHttpRequest;//var xhr=null;//try{//    if(window.XMLHttpRequest){//        xhr=new XMLHttpRequest;//    }else if(new ActiveXObject("Microsoft.XMLHTTP")){//        xhr=new ActiveXObject("Microsoft.XMLHTTP");//    }else if(new ActiveXObject("Msxm12.XMLHTTP")){//        xhr=new ActiveXObject("Msxm12.XMLHTTP");//    }else if(new ActiveXObject("Msxm13.XMLHTTP")){//        xhr=new ActiveXObject("Msxm13.XMLHTTP");//    }////}catch(e){//    throw new Error("您當前瀏覽器不支援AJAX~ 破電腦砸了吧!!!");//}//第一次升級v//惰性思想:在自執行函數執行之後我們把getXHR賦值為一個具體的函數,這樣以後執行這個方法時,就不用在判斷是什麼瀏覽器了//var getXHR=null;//-function(){//    var list=[function(){//        return new XMLHttpRequest;//    },function(){//        return new ActiveXObject("Microsoft.XMLHTTP");//    },function(){//        return new ActiveXObject("Msxm12.XMLHTTP");//    },function(){//        return new ActiveXObject("Msxm13.XMLHTTP");//    }];//    var temp=null;//    for(var i=0;i<list.length;i++){//        try{//            temp=list[i]();//        }catch(e){//            continue;//        }//        getXHR=list[i];//        break;//    }//    if(!temp){//        throw new Error("您當前瀏覽器不支援AJAX~ 破電腦砸了吧!!!");//    }//}();var getXHR=function(){    var list=[function(){        return new XMLHttpRequest;    },function(){        return new ActiveXObject("Microsoft.XMLHTTP");    },function(){        return new ActiveXObject("Msxm12.XMLHTTP");    },function(){        return new ActiveXObject("Msxm13.XMLHTTP");    }];    var temp=null;    for(var i=0;i<list.length;i++){        try{            temp=list[i]();        }catch(e){            continue        }        getXHR=list[i];        break    }    if(!temp){        throw new Error("您當前瀏覽器不支援AJAX~ 破電腦砸了吧!!!");    }    return temp};var xhr=getXHR();//2. 開啟一個URL地址(伺服器給我們的資料請求的一個介面API)//第一個參數請求方式get post put delete// get: 一般用來向伺服器發送一個請求,從伺服器擷取內容// post:一般應用於客服端給伺服器推送大量的內容// get 通過URL請求向伺服器傳參數,並在?的後面拼上需要傳的參數// post 通過xhr.send(字串)//xhr.send(‘{"name":"xiaoming","age":12}‘);// 長度:get通過URL傳參數,但是瀏覽器對於URL的大小是有限制的(一般Google8KB Firefox7KB IE2KB),//        一旦超出瀏覽器會自動減去,這樣傳給背景資料就不完整了,這種情況只能用post// 安全:get通過URL傳參數可以在控制台看見,安全性不高  post 是把傳遞的內容放在HTTP主題中 在控制台看不到 安全性更高// 緩衝問題:post請求瀏覽器不會預設記錄緩衝資訊,也不需要清除緩衝// get請求瀏覽器會預設記錄緩衝資訊,一般我們會在URL拼接參數的時候給他一個時間戳記或者隨機數,這樣每次請求的地址就會不一樣(不一樣就不緩衝)var time=new Date();var times=time.getTime();console.log(times);var url="http://www.com.qq.com";var code=Math.random();var strCode=url.indexOf("?")>-1?url+"time="+times:url+"?time="+times;console.log(strCode);//3. 監聽資料請求//xhr.readyState:ajax請求狀態代碼//0 未發送請求  1 已開啟串連   2 已擷取回應標頭部資訊   3 請求的資料內容正在載入    4 請求完成得到資料//xhr.status:網路狀態代碼//200 一般以2開頭  正常     404 找不到 檔案,圖片...不存在      502 伺服器錯誤xhr.onreadystatechange=function(){    if(xhr.readyState===4&&/^2\d{2}$/.test(xhr.status)){        var str=xhr.responseText;        //接收後台返回來的JSON字串    }};xhr.send(null);function Ajax(url,callback){    var xhr=getXHR();    url+=url.indexOf("?")>-1?"time="+(new Date).getTime():"?time="+(new Date).getTime();    xhr.open("get",url,true);//同步false   非同步true    xhr.onreadystatechange=function(){        if(xhr.readyState===4&&/^2\d{2}$/.test(xhr.status)){            var str=xhr.responseText;            str="JSON" in window?JSON.parse(str):eval("("+str+")");            callback(str);        }    }    xhr.send(null);}

 

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.