简单的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;
}
}
} 相关推荐
wcqwcq 2020-06-26
delmarks 2020-06-14
ppsurcao 2020-06-14
tthappyer 2020-06-07
kentrl 2020-11-10
结束数据方法的参数,该如何定义?-- 集合为自定义实体类中的结合属性,有几个实体类,改变下标就行了。<input id="add" type="button" value="新增visitor&quo
ajaxyan 2020-11-09
zndy0 2020-11-03
学留痕 2020-09-20
learningever 2020-09-19
chongxiaocheng 2020-08-16
ajaxhe 2020-08-16
lyqdanang 2020-08-16
curiousL 2020-08-03