原生Js封装Get,Post请求和ajax整体封装和测试

/*ajax封装-get
       @param url:请求的路径
       @param data:发送的数据,格式:key1=value1&key2=value2
       @param callback:回调函数,方便用户处理自己的数据,传递逻辑
*/

function get(url, data, callback){
       //创建异步对象
       var xhr = new XMLHttpRequest();
       //判断data是否为空
       if(data){
           url=url+'?'+data;
       }
       //设置请求行
       xhr.open('get',url);
       //设置请求头(get可以省略)
       //注册回调函数
       xhr.onreadystatechange = function(){
            if(xhr.readyState==4&&xhr.status==200){
                //调用传递的回调函数
                callback(xhr.responseText);
            }
       }
       //发送请求主体
       xhr.send(null);
}

/*ajax封装-post
       @param url:请求的路径
       @param data:发送的数据,格式:key1=value1&key2=value2
       @param callback:回调函数,方便用户处理自己的数据,传递逻辑
*/

function post(url, data, callback){
       //创建异步对象
       var xhr = new XMLHttpRequest();
       //设置请求行
       xhr.open('post',url);
       //设置请求头(post有数据发送才需要设置请求头)
       //判断是否有数据发送
       if(data){
             xhr.setRequestHeader('content-type','application/x-www-form-urlencoded');
       }
       //注册回调函数
       xhr.onreadystatechange = function(){
             if(xhr.readyState==4&&xhr.status==200){
                 //调用传递的回调函数
                 callback(xhr.responseText);
             }
       }
       //发送请求主体
       xhr.send(data);
}

/*ajax封装
       @param option:传入一个对象
       属性分别为(顺序可以打乱):
            url:请求的路径
            type:请求的不同类型get或post
            data:发送的数据,格式:key1=value1&key2=value2
            callback:回调函数,方便用户处理自己的数据,传递逻辑           
*/

function ajax(option){
       //创建异步对象
       var xhr = new XMLHttpRequest();
       //如果是get并且有数据
       if(option.type=='get'&&option.data){
            option.url=option.url+'?'+option.data;
       }
       //设置请求行
       xhr.open(option.type,option.url);
       //设置请求头(post有数据发送才需要设置请求头)
       //判断是否有数据发送
       if(option.type=='post'&&option.data){
             xhr.setRequestHeader('content-type','application/x-www-form-urlencoded');
       }
       //注册回调函数
       xhr.onreadystatechange = function(){
             if(xhr.readyState==4&&xhr.status==200){
                 //接收返回的数据类型
                 var type = xhr.getResponseHeader('Content-Type');
                 //json格式
                 if(type.indexOf('json')!=-1){
                      option.callback(JSON.parse(xhr.responseText));
                 }
                 //xml格式
                 else if(type.indexOf('xml')!=-1){
                      option.callback(xhr.responseXML);
                 }
                 //普通格式
                 else{
                      option.callback(xhr.responseText);
                 }
             }
       }
       //发送请求主体
       //判断不同的请求类型
       xhr.send(option.type=='post'?option.data:null);
}

测试封装的效果代码html:

<!DOCTYPE html>
<html>
        <head>
                 <title>测试ajax封装</title>   
                 <meta charset="UTF-8" />   
        </head>
        <body>
                  <h1>测试ajax封装</h1>
                  <input class="get" type="button" value="get测试"/>
                  <input class="post" type="button" value="post测试"/>
                  <input class="ajax" type="button" value="ajax测试"/>

                  <script src="ajax_tool.js"></script>
                  <script>
                          //get
                          document.querySelector('.get').onclick = function(){
                               //直接调用get方法,传入三个参数url,data,callback
                               get('./ajax-test.php','name=jackson&friend=german',function(data){
                                     console.log(data);
                               });
                          }
                          //post
                          document.querySelector('.post').onclick = function(){
                               //直接调用post方法,传入三个参数url,data,callback
                               post('./ajax-test.php','name=german&friend=jackson',function(data){
                                     console.log(data);
                               });
                          }
                          document.querySelector('.ajax').onclick = function(){
                               //直接调用ajax方法,传入一个对象
                               ajax({
                                    url:'./ajax-test.php',
                                    type:'post',
                                    data:'kill=电脑&hobby=运动',
                                    callback:function (data){
                                          console.log(data);
                                    }
                               });
                          }

                       </script>
        </body>
</html>

php服务器代码:

<?php
    echo '<h2>打印get请求</h2>';
    print_r($_GET);
    echo '<h2>打印post请求</h2>';
    print_r($_POST);
?>

在测试之前,你可以先去官网 http://phpstudy.php.cn/ 下载phpstudy启动apache服务器,

然后在浏览器地址栏输入127.0.0.1找到html文件运行:

如下图片:

get请求

post请求

ajax-get请求

ajax-post请求

(有什么问题欢迎留言~~)

 

 

 

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值