微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

GET和POST请求的封装

GET请求的封装

<body>
<script src="/ajax.js"></script>
  <script>
    ajax.get("1.PHP",{"id":1,"name":"小明","age":18},function(value){
      console.log(value)
    });
  </script>
</body>

ajax.js

(function(){
    // 唯一暴露的参数变量
    window.ajax = ajax = {};//给Windows的ajax赋一个值,这个值是一个变量名,这个变量名的对象
    console.log(window)
    ajax.get = function(url,json,callback) {
        console.log(url,json,callback)
    }
  })()

 

 此时我们就可以拿到数据,通过ajax.get传入参数,我们可以输出window属性,可以看到这个get函数,其中携带着我们传入的参数

 

1.html文件

<!DOCTYPE html>
<html lang="en">
<head>
  <Meta charset="UTF-8">
  <Meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
    <div id="Box"></div>
<script src="/ajax.js"></script>
  <script>
   var Box=document.getElementById("Box");
   ajax.get("1.PHP",{"id":1,"name":"小明","age":18},function(value){
       console.log(value)
   })
  </script>
</body>
</html>

ajax.js文件

(function(){
    // 唯一暴露的参数变量
    window.ajax = ajax = {};//给Windows的ajax赋一个值,这个值是一个变量名,这个变量名的对象
    console.log(window)
    ajax.get = function(url,JSON,callback) {
        var xhr=new XMLHttpRequest();
        xhr.onreadystatechange=function(){
            if(xhr.readyState==4){
                if(xhr.status>=200&&xhr.status<=300||xhr.status==304){
                    callback(xhr.responseText)
                }
            }
        }
        // 拼接JSON数据,比如我们的参数{"id":1,"name":"小明","age":18}
         // 转换为id=10001&name=小明&age=18
         var temp = [];
         for(var k in JSON) {
           temp.push(k+"="+encodeURI(JSON[k]));
         }
         // 将temp的数据转换为字符串格式的,共最后提交请求使用
         var str = temp.join("&");
         // 防止没有参数
         if(str) {
           url+="?"+str
         }
         xhr.open("get",url,true);
         xhr.send(null);
       
          
    }
  })()
  

 

 三个参数情况

 ajax.get("1.PHP",{"id":1,"name":"小明","age":18},function(value){
       console.log(value)
   })

两个参数情况

ajax.get("1.PHP",function(value){
       console.log(value)
   })

如果传入的参数是两个,那么需要设置一个判断信息

在ajax.js文件中的ajax.get函数添加一个判断信息

   // 如果用户只传了两个参数,第二个参数如果不是JSON就是函数
         // 如果第二个参数的类型是函数了,说明第二个参数就是回调函数
         if(typeof JSON == "function") {
           // 如果第二个参数是回调函数了,让callback参数就等于这个函数
           callback = JSON;
           JSON = {};
         }

 

POST请求

POST请求实际上和GET请求的内容基本相同

 ajax.post=function(url,JSON,callback){ 
        var xhr=new XMLHttpRequest();
        xhr.onreadystatechange=function(){
            if(xhr.readyState==4){
                if(xhr.status>=200&&xhr.status<=300||xhr.status==304){
                    callback(xhr.responseText)
                }
            }
        }
          // 拼接JSON数据,比如我们的参数{"id":1,"name":"小明","age":18}
         // 转换为id=10001&name=小明&age=18
         var temp = [];
         for(var k in JSON) {
           temp.push(k+"="+encodeURI(JSON[k]));
         }
         var str=temp.join("&")||null
         xhr.open("post",url,true);
         xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
         xhr.send(str)
       
    }
ajax.post("1.PHP",{"id":1,"name":"小明","age":18},function(value){
       console.log(value)
   })

 

 传两个参数或者三个参数的解决方法跟get请求一样。

                                                                                                                                                                                                                                                                                                                           

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐