1. 程式人生 > >post及get請求區別

post及get請求區別

 1 <script type="text/javascript">
 2 function ajax(){
 3     var XHR=null;  
 4 
 5     //第一步:建立XMLHttpRequest物件
 6     if (window.XMLHttpRequest) {  
 7         XHR = new XMLHttpRequest();  // 非IE核心:IE7+, Firefox, Chrome, Opera, Safari  
 8     } else if (window.ActiveXObject) {  
 9         XHR =
new ActiveXObject("Microsoft.XMLHTTP"); // IE核心:IE6, IE5 10 } else { 11 XHR = null; 12 } 13 14 //第二步:傳送請求 15 if(XHR){ 16 XHR.open("POST", '1.php?='+Math.random(),true); //"/test/ajax"test為專案名稱、ajax為方法名稱 17 18 //第三步:設定Content-Type 19 XHR.setRequestHeader(
"Content-type","application/x-www-form-urlencoded"); 20 21 //第四步: 22 XHR.onreadystatechange = function () { 23 // readyState值說明 24 // 0,初始化,XHR物件已經建立,還未執行open 25 // 1,載入,已經呼叫open方法,但是還沒傳送請求 26 // 2,載入完成,請求已經發送完成 27 // 3,互動,可以接收到部分資料,因為相應及http頭不全,這時通過responseText獲取部分資料會出現錯誤
28 // 4,資料接收完成,此時可以通過responseText獲取完整的資料 29 30 // status值說明 31 // 200:成功 32 // 404:沒有發現檔案、查詢或URl 33 // 500:伺服器產生內部錯誤 34 if (XHR.readyState == 4 && XHR.status == 200) { //注① 35 // 這裡可以對返回的內容做處理 36 // 一般會返回JSON或XML資料格式 37 console.log("XHR.responseText-->"+XHR.responseText); 38 // 主動釋放,JS本身也會回收的 39 XHR = null; 40 } 41 }; 42 XHR.send('name=mino&age=25'); 43 } 44 } 45 </script>