Ajax --- 資料請求

來源:互聯網
上載者:User

標籤:get   回呼函數   orm   pre   send   登入   admin   www   ros   

下面主要介紹(JS原生)資料請求的主要步驟:

Ajax 資料請求步驟:

1、建立XMLHttpRequest對象
2、準備資料發送
3、執行發送
4、指定回掉函數

 

第一步:建立XMLHttpRequest對象

1 var xhr = new XMLHttpRequest();                            //  標準瀏覽器2                     3 var xhr = new ActiveXObject(‘Microsoft.XMLHTTP‘);          //  IE6

 

第二步:使用 open() 方法將參數傳入

1 xhr.open(‘get‘,‘./check.php?username=‘+uname+‘&password=‘+pw,true);   // get 請求方式2 3 xhr.open(‘post‘,‘./01check.php‘,true);                                // post 請求方式

 

第三步:使用 send() 方法將請求發送值伺服器

1 xhr.send(null);       // get 請求方式時,此處值為 null2 3 xhr.send(請求地址);   // post 請求方式時,此處值為 請求地址

 

第四步:執行回掉函數

1 xhr.onreadystatechange = function(){            2   if(xhr.readyState == 4){        // 4表示伺服器返回的資料已經可以使用了,但是這個資料不一定是正常的3     if(xhr.status == 200){      // 這裡的200表示資料是正常的4        alert(xhr.responseText);5     }6   }7 }8 </script>

 

綜合:

 1 ==================================== html 頁面  ============================================ 2     <form> 3   使用者名稱: 4         <input type="text" name="username" id="username"><span id="info"></span> 5         <br> 密碼: 6         <input type="text" name="password" id="password"> 7         <input type="button" value="登入" id="btn"> 8     </form> 9 10 <script>11 var uname = document.getElementById(‘username‘).value;12 var pw = document.getElementById(‘password‘).value;13 14 // 1、建立XMLHttpRequest對象15 var xhr = null;16 if(window.XMLHttpRequest){17      xhr = new XMLHttpRequest();//標準18 }else{19      xhr = new ActiveXObject("Microsoft");//IE620 }21 22 // 2、準備發送23 xhr.open(‘post‘,‘post.php‘,true);24 25 // 3、執行發送動作26 xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");   // 如 get 此步驟省略27 xhr.send(‘username=‘+uname+‘&password=‘+pw);               //post請求參數在這裡傳遞,並且不需要轉碼28 29 // 4、指定回呼函數30 xhr.onreadystatechange = function(){            31 if(xhr.readyState == 4 && xhr.status == 200){        // 4表示伺服器返回的資料已經可以使用了,這裡的200表示資料是正常的32      var data = xhr.responseText;33      if(data == ‘1‘){34           info.innerHTML == ‘登入成功‘;35      }36      else if(data == ‘2‘){37           info.innerHTML == ‘使用者名稱或密碼錯誤‘;38      }else{39           info.innerHTML == ‘伺服器錯誤‘;40      }41 }42 }43 </script>44 45 =========================================== post.php 頁面 ========================================46 <?php47     $uname = $_POST[‘username‘];48     $pw = $_POST[‘password‘];49 50     if($uname == ‘admin‘ && $pw == ‘123‘){51         echo 1;52     }else{53         echo $uname;54     }55  ?>

 註:以上是個人對原型鏈的理解及總結,如有筆誤的地方還請大家多指教!

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.