源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

简单的ajax连接库分享(不用jquery的ajax)

  • 时间:2020-10-25 16:13 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:简单的ajax连接库分享(不用jquery的ajax)
[u]复制代码[/u] 代码如下:
var ajax = {  init : function(){   var xmlHttp = new XMLHttpRequest();   if (!window.XMLHttpRequest)      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");     return xmlHttp;  },  call : function(opt){   var xmlHttp = this.init();   xmlHttp.onreadystatechange = function(){    if(xmlHttp.readyState===4)    {      xmlHttp.status===200 ?      opt.success(xmlHttp.responseText,xmlHttp.responseXML) : opt.error(xmlHttp.responseText,xmlHttp.status);    }   }   opt.data = this.parseData(opt.data);   if(opt.method.toLowerCase() === 'get'){    opt.url = opt.url + "?" + opt.data;    opt.data = null;   }   xmlHttp.open(opt.method,opt.url,opt.async);   if(opt.method.toLowerCase() === 'post')    xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");   xmlHttp.send(opt.data);  },  parseData : function(data){   if(typeof data == 'object'){    var str = '';    for(var i in data){     str += "&"+i+"="+encodeURIComponent(data[i]);    }    return str.length==0 ? str : str.substring(1);   }else{    return data;   }  } }
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部