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

时间:2014-01-19    点击:117   

复制代码 代码如下:

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

js控制input框只读实现示例
js给页面加style无效果的解决方法
可自定义速度的js图片无缝滚动示例分享
文本框文本自动补全效果示例分享
不使用jquery实现js打字效果示例分享
> 返回     
地址:上海市普陀区胶州路941号长久商务中心 电话: QQ:
© Copyright 2012 上海网络 Product All Rights Reserved