js post 非同步請求

來源:互聯網
上載者:User
昨天在修複一個bug的時候,使用jquery post資料的時候發現post請求總是沒有發出去,nnd,難道是jquery庫問題啊,於是自己寫了一個xmlhttp的方法,然後調試了一下,發現ok,那是估計需要更新一下jquery的庫了,這個還沒有調試,先用自己的了;以下是xmlHttp的coding:var xmlHttp;function createXMLHttpRequest(){    //Mozilla 瀏覽器(將XMLHttpRequest對象作為本地瀏覽器對象來建立)    if(window.XMLHttpRequest){ //Mozilla 瀏覽器        xmlHttp = new XMLHttpRequest();    }else if(window.ActiveXObject) { //IE瀏覽器    //IE瀏覽器(將XMLHttpRequest對象作為ActiveX對象來建立)        try{            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");        }catch(e){            try {                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");            }catch(e){}        }    }    if(xmlHttp == null){        alert("不能建立XMLHttpRequest對象");        return false;    }}//用於發出非同步請求的方法function sendAsynchronRequest(url,parameter,callback){    createXMLHttpRequest();    if(parameter == null){        //設定一個事件處理器,當XMLHttp狀態發生變化,就會出發該事件處理器,由他調用        //callback指定的javascript函數        xmlHttp.onreadystatechange = callback;        //設定對拂去其調用的參數(提交的方式,請求的的url,請求的類型(非同步請求))        xmlHttp.open("GET",url,true);//true表示發出一個非同步請求。        xmlHttp.send(null);    }else{        xmlHttp.onreadystatechange = callback;        xmlHttp.open("POST",url,true);        xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");        xmlHttp.send(parameter);    }}//以上代碼是通用的方法,接下來是調用以上的方法function loadPros(title,count,pid,cid,level){    // 調用非同步要求方法    url = "。。。。。。。。";    sendAsynchronRequest(url,null,loadCallBack);}// 指定回調方法function loadCallBack(){  try {    if (xmlHttp.readyState == 4) {        if (xmlHttp.status == 200) {            if(xmlHttp.responseText != null && xmlHttp.responseText != ""){               var divProid = document.getElementById('videolist');               divProid.innerHTML = xmlHttp.responseText;               for(i=0;i<len;i++)               {                   var video_url = document.getElementById("videolist"+i+"").href;                   if(video_url != undefined && video_url != null && video_url != ""){                      window.location.href = video_url;                   }               }           }        }     }     if (xmlHttp.readyState == 1)     {        //alert("正在載入連線物件......");     }     if (xmlHttp.readyState == 2)     {        //alert("連線物件載入完畢。");     }     if (xmlHttp.readyState == 3)     {        //alert("資料擷取中......");     }  }  catch (e)  {      //alert(e);  }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.