ajax中的同步非同步

來源:互聯網
上載者:User

標籤:

請求方式,分為GET與POST: GET 最為常見的HTTP請求,普通上網瀏覽頁面就是GET。GET方式的參數請求直接跟在URL後,以問號開始。(JS中用window.location.search獲得)。參數可以用encodeURIComponent進行編碼,使用方式: 

var EnParam = encodeURIComponent(param);

URL只支援大約2K的長度,即2048字元數;使用GET進行AJAX請求時候會緩衝導致出現的頁面不是正確的,一般方法加random參數值;ajax.send(null)。

POST

向伺服器提交資料用到。

需要將form表單中的值先取出轉換成字串,用&符號串連,(同GET傳參數一樣);提交資料量2GB ;使用ajax.setRequestHeader(‘Content-Type‘, ‘application/x-www-form-urlencoded‘),處理提交的字串;ajax.send(strings),這個strings表示form中需要提交的內容,例如a=1&b=2類似這樣的字串。

 

同步與非同步:

ajax.open方法中,第3個參數是設同步或者非同步。prototype等js類庫一般都預設為非同步,即設為true。先說下同步的情況下,js會等待請求返回,擷取status。不需要onreadystatechange事件處理函數。而非同步則需要onreadystatechange事件處理,且值為4再正確處理下面的內容。

(註:文中的 ajax 表示XMLHTTP請求對象。)

 1 Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> 1 //同步傳輸模式 2  3 function RequestByGet(nProducttemp,nCountrytemp) 4 { 5     var xmlhttp 6  7     if (window.XMLHttpRequest)   8     {   9          //isIE   =   false;  10          xmlhttp   =   new   XMLHttpRequest();  11     }  12     else if (window.ActiveXObject)13     {  14          //isIE   =   true;  15          xmlhttp   =   new   ActiveXObject("Microsoft.XMLHTTP");  16     }17                  18     //Web page location.19     var URL="http://www.baidu.com/;20     xmlhttp.open("GET",URL, false);21     //xmlhttp.SetRequestHeader("Content-Type","text/html; charset=Shift_JIS")22     xmlhttp.send(null);23     var result = xmlhttp.status;24    25     //OK26     if(result==200)27     {28         document.getElementById("div_RightBarBody").innerHTML=xmlhttp.responseText;29     }30     xmlhttp = null;31 }32 33 34 //非同步傳輸模式35 var xmlhttp36 37 function RequestByGet(nProducttemp,nCountrytemp)38 {39     if (window.XMLHttpRequest)  40     {  41          //isIE   =   false;  42          xmlhttp   =   new   XMLHttpRequest();  43     }  44     else if (window.ActiveXObject)45     {  46          //isIE   =   true;  47          xmlhttp   =   new   ActiveXObject("Microsoft.XMLHTTP");  48     }49                  50     //Web page location.51     var URL="http://www.baidu.com/";52     xmlhttp.open("GET",URL, true);53     xmlhttp.onreadystatechange = handleResponse;54     //xmlhttp.SetRequestHeader("Content-Type","text/html; charset=UTF-8")55     xmlhttp.send(null);  56 }57 58 function handleResponse()59 {60     if(xmlhttp.readyState == 4 && xmlhttp.status==200)61     {62         document.getElementById("div_RightBarBody").innerHTML=xmlhttp.responseText;63         xmlhttp = null;64     }65 }

 

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.