封装ajxa 新的笔记

function Ajax(){
            //第一步:创建xhr对象
            //根据实际情况,通过不同方式创建xhr
            var xhr=null;
            if (window.XMLHttpRequest) {
                xhr=new XMLHttpRequest();
            }else{
                xhr=new ActiveXObject('Microsoft.XMLHttp')
            }

            //第二步:开始监听
            //创建完 xhr对象后,它的工作就是 开始监听
            //下面这个函数里 就2个if判断句
            xhr.onreadystatechange=function (){
                if (xhr.readyState == 4) {
                    if(xhr.status == 200) {
                        callback(xhr.responseText);
                    }else{
                        console.log('error');
                    }
                }
            }

            //第三步:对发送过来的请求(get、post)给出回应……
            method=method.toUpperCase();
            if (method=='GET') {
                var date=new Date(),
                    timer=date.getTime();
                xhr.open(method, url+'?'+data+'&timer'+time, flag);
                xhr.send();
            }else {
                xhr.open(method,url,flag);
                xhr.setRequestHeader('Content-type','application//x-www-form-urlencoded');
                xhr.send(data);
            }
        }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值