我是靠谱客的博主 背后手机,这篇文章主要介绍简单的ajax连接库分享(不用jquery的ajax),现在分享给大家,希望可以做个参考。

复制代码 代码如下:
复制代码

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;
  }
 }
}

最后

以上就是背后手机最近收集整理的关于简单的ajax连接库分享(不用jquery的ajax)的全部内容,更多相关简单内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(125)

评论列表共有 0 条评论

立即
投稿
返回
顶部