前端进阶Ajax-----GET和POST请求

  <script>
        // get请求
        var xhr = new XMLHttpRequest();

        // 需要携带参数
        // 因为是GET请求,直接在地址后面进行参数的书写
        xhr.open('GET',"http://locahost:888/test/second?name=小不言&age=18",true);

        xhr.onload = function(){
            console.log(JSON.parse(xhr.responseText));
        }
        xhr.send();





        // post请求
        var xhr = new XMLHttpRequest();

        xhr.open('post','http://lcoalhost:8888/test/firte',true);

        xhr.onload = function(){
            console.log(JSON.parse(xhr.responseText));
        }

        // 注意:当你发送 POST 请求,并且需要携带参数的时候,需要进行特殊说明
        // 语法:xhr.setRequestHeader('content-type',你传递参数的格式);
        xhr.setRequestHeader('content-type','application/x-www-form-urlencoded');

        // sed后边的()就是书写请求体的位置
        xhr.send('name=小不言&age=18');


    </script>


版权声明:本文为m0_61656944原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。