AJAX_get請求和post請求執行個體代碼

來源:互聯網
上載者:User

標籤:aaa   color   als   XML   資料   urlencode   字元   pen   處理   

GET 請求:

<script>    function getData(){        //1.建立ajax對象        var xhr = new XMLHttpRequest();        //2、給ajax狀態綁定狀態改變的監聽函數,在函數中判斷狀態等於4開始接受資料        xhr.onreadystatechange=function(){            if(xhr.readyState == 4){                //根據返回的結果開始商務邏輯            }        }        //3、.建立一個http請求,指定伺服器的url地址        xhr.open(‘get‘,"./response.php?name=fm",true); //第三個參數true是非同步,false就是同步        //4、.發送一個http請求        xhr.send(null); //get 請求參數直接null , post請求需要拼接請求的字串如:name=aaa&age=24    }</script>

POST請求:

<script>    function postData(){        //1.建立ajax對象        var xhr = new XMLHttpRequest();        //2、給ajax的狀態改變綁定一個監聽函數,監聽狀態等於4,接受資料,進行商務邏輯處理        xhr.onreadystatechange=function(){            if(xhr.readyState == 4){                    //根據返回的結果開始商務邏輯            }        }        //3.建立一個http串連,這裡的參數依舊要用get方式接收        xhr.open(‘post‘,‘reponse.php?address=beijinglu‘,true);        //4.設定post要求標頭,作用類比form表單來提交資料        xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");        //5.發送ajax請求,這裡的參數要通過post接收        xhr.send("name=xiaoming&age=100");    }</script>

 

AJAX_get請求和post請求執行個體代碼

相關文章

聯繫我們

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