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); }}