自己封装ajax,json.

var Ajax = (function(){
        //加载json
        if(typeof JSON == "undefined"){
                var script = document.createElement("script");
                script.type = "text/javascript";
                //替换成静态地址
                script.src="json2.js";
                document.getElementsByTagName("head")[0].appendChild(script);
        }
        //查找功能函数
        function findFrom(){
            var returnValue;

            for (var i = 0, length = arguments.length; i < length; i++) {
              var lambda = arguments[i];
              try {
                returnValue = lambda();
                break;
              } catch (e) { }
            }

            return returnValue;
        }
        //xml http request取得ajax的请求工具
        var httpRequest = function(){
                var returnValue = false;
                return findFrom(
                        function() {return new XMLHttpRequest()},
                    function() {return new ActiveXObject('Msxml2.XMLHTTP')},
                        function() {return new ActiveXObject('Microsoft.XMLHTTP')}
            )||false;       
        }();
       
        var proto = {
                        method:null,
                        dataType:"JSON",
                       
                        post:function(params){
                                this.method = "POST";
                                this._send(params);
                        },
                        get:function(params){
                                this.method = "GET";
                                this._send(params);
                        },
                        _send:function(data){
                                var strData = this._serialize(data);
                                if(this.method == "GET"){
                                        this.url=this.url+"?"+strData;
                                }
                               
                                this.HttpRequest.open(this.method,this.url,this.async);
                                //监听事件
                                var r = this.HttpRequest,self = this;
                                r.onreadystatechange=function(){
                                        console.log(r.readyState);
                                  if (r.readyState==4 && r.status==200){
                                          try{
                                                  var data = null;
                                                  if(this.dataType.toUpperCase() == "JSON"){
                                                          data = eval(r.responseText);
                                                  }else{
                                                          data = r.responseText;
                                                  }
                                          }catch(e){
                                                  'error';
                                          }
                                          self.onSuccess(data);
                                  }
                                 }
                                self.onLoading();
                                if(this.method == "POST"){
                                        this.HttpRequest.setRequestHeader("Content-type","application/x-www-form-urlencoded");
                                        this.HttpRequest.send(strData)
                                }else{
                                        this.HttpRequest.send();
                                }
                               
                        },
                        _serialize:function(data){
                                var values = [];
                                for(var key in data){
                                        var value = data[key];
                                        if(typeof value =="object"){
                                                value = jSON.stringify(value);
                                        }
                                        values.push(key+'='+value);
                                }
                                return values.join("&");
                        },
                        onSuccess:function(){
                                if(typeof this.success == "function"){
                                        this.success();
                                }
                        },
                        onLoading:function(){
                                if(typeof this.loading == "function"){
                                        this.loading();
                                }
                        }
        }
        //console.log(httpRequest);
        //主函数
//        var props = {url:async:dataType:}
       
        var A = function(props){
                this.HttpRequest = httpRequest;
                for(var key in props){
                        proto[key] = props[key];
                }
        }
        //扩展a
        A.prototype = proto;
       
        return A;
})();


//function sendData(){
//        //用法
//        alert("aaa");
//        var a = new Ajax({url:"http://*.php",dataType:"json",onSuccess:function(){console.log("成功了");},onLoading:function(){console.log("发送中");}});
//        a.get({data:11111});
//        a.post({data:2222});
//}
//a.get();a.post();a.error();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

rorg

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值