zoukankan      html  css  js  c++  java
  • ajax 封装

    ajax基本写法,简单够用

    //ajax
    function Ajax(o){
        this.data={
            "url":"",
            "type":"POST",
            "dataType":"json",
            "data":"",
            "async":true,
            "success":function(e){},
            "err":function(e){}
        };
        for(var i in o){ this.data[i]=o[i]};
        this.fn={
            "jsonof" : function(obj){
                var isjson = typeof(obj) == "object" && Object.prototype.toString.call(obj).toLowerCase() == "[object object]" && !obj.length; 
                return isjson;
            },
            "setUrlK" : function setUrlK(ojson) {
                var s='',name, key;
                for(var p in ojson) {
                    if(!ojson[p]) {return;} 
                    if(ojson.hasOwnProperty(p)) { name = p };
                    key = ojson[p];
                    s += "&" + name + "=" + encodeURIComponent(key);
                };
                return s.substring(1,s.length);
            }
        };
        this.go_data="";
        if(this.data.data){
            if( this.fn.jsonof(this.data.data) ){
                this.go_data = encodeURI(this.fn.setUrlK(this.data.data));
            }else if( typeof this.data.data ==='string'){
                this.go_data = encodeURI(this.data.data);
            }else{ return null;};
        };
        this.init();
    };
    Ajax.prototype.init=function(){
        this.oAjax=window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');//1
        if(this.data.type.toUpperCase() === "GOT"){
            if(this.go_data){
                this.oAjax.open(this.data.type , this.data.url+"?"+ this.go_data, this.data.async);//2
            }else{
                this.oAjax.open(this.data.type , this.data.url , this.data.async);//2
            };
        }else {
            this.oAjax.open(this.data.type , this.data.url , this.data.async);//2
            this.oAjax.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
        };
        this.oAjax.send( this.data.type.toUpperCase() === "GOT" ? null : this.go_data );//3
        
        var _this=this;
        this.oAjax.onreadystatechange=function(){_this.changefn()};//4
    };
    Ajax.prototype.changefn=function(){
        if(this.oAjax.readyState===4) {
            this.statusCode=this.oAjax.status
            if(this.statusCode===200){
                this.data.success( JSON.parse(this.oAjax.responseText) );
            }else{
                this.data.err(this.statusCode);
            };
        };
    };
    
    //应用 ===========================
    new Ajax({
        url:"http://shiyong.jiankang.163.com/user/msg/list.html",
        type:'POST',
        dataType:'json',
        data:{name:"宋",age:28},
        success:function(e){
            console.log(e.result)
        },
        err:function(e){
            console.log(e)
        }
    });
  • 相关阅读:
    用kettle做ETL时设置mysql连接参数使数据写入速度加快
    infobright社区版rpm包
    mysql在大数据量下性能调优相关参数
    greenplum给某个用户赋予整个schema下所有表的权限
    Linux挂载大于2T的磁盘硬盘
    Centos 系统swap(虚拟内存)管理
    域内
    随便记录一些东西
    有关终端的一些tips
    精悍的指令
  • 原文地址:https://www.cnblogs.com/SongYiJian/p/5682616.html
Copyright © 2011-2022 走看看