用JS写的一个Ajax库(实例代码)
myajax是一个用js编写的一个跨浏览器的ajax库,支持get,post,jsonp请求,精巧,简单。
一、发送GET请求:
myajax.get({
<spanstyle="white-space:pre"> </span>data:{},//参数
url:"",//请求地址
//发生错误是调用
error:function(data){
},
//请求成功调用
success:function(data){
<spanstyle="white-space:pre"> </span>//eval(data);将字符串转换成json
}
});
二、发送POST请求:
myajax.post({
data:{},//参数
url:"",//
//发生错误是调用
error:function(data){
},
//请求成功调用
success:function(data){
//eval(data);将字符串转换成json
}
});
三、发送JSONP请求:
myajax.getJSONP({
//参数
data:{
},
url:"",//请求地址
//请求成功调用
success:function(data){
},
//发生错误时调用
error:function(){
}
});
源码:
varmyajax={
post:function(params){
varxmlhttp=this.createXMLHttpRequest();
if(xmlhttp!=null)
{
varasync=true;
if(typeofparams.async!="undefined")
async=params.async;
vardata=null;
if(typeofparams.data!="undefined")
data=params.data;
varurl="";
if(typeofparams.url!="undefined")
url=params.url;
if(url==null||url.length==0)
return;
xmlhttp.open("POST",url,async);
if(async){
xmlhttp.onreadystatechange=function(){
if(this.readyState==4){
if(this.status==200){
if(typeofparams.success!="undefined"){
params.success(xmlhttp.responseText);
}
}
else{
if(typeofparams.error!="undefined"){
params.error(xmlhttp.status+xmlhttp.statusText);
}
console.error(url+":"+xmlhttp.status);
}
}
};
}
xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
varparam="";
for(varpropindata){
param+=prop+"="+data[prop]+"&";
}
param=param.substring(0,param.length-1);
xmlhttp.send(param);
if(!async){
if(xmlhttp.readyState==4&&xmlhttp.status==200)
if(typeofparams.success!="undefined"){
params.success(xmlhttp.responseText);
}
else{
if(typeofparams.error!="undefined"){
params.error(xmlhttp.status+xmlhttp.statusText);
}
console.error(url+":"+xmlhttp.status);
}
}
}
},
get:function(params){
varxmlhttp=this.createXMLHttpRequest();
if(xmlhttp!=null)
{
varasync=true;
if(params.async!=undefined)
async=params.async;
varurl="";
if(params.url!=undefined)
url=params.url;
if(url==null||url.length==0)
return;
if(params.data!=null){
vardata=params.data;
varparamPrefix=url.indexOf("?")==-1?"?":"&";
url=url+paramPrefix;
for(varpropindata){
url+=prop+"="+data[prop]+"&";
}
url=url.substring(0,url.length-1);
}
xmlhttp.open("GET",url,async);
if(async){
xmlhttp.onreadystatechange=function(){
if(this.readyState==4){
if(this.status==200){
if(typeofparams.success!="undefined"){
params.success(xmlhttp.responseText);
}
}
else{
if(typeofparams.error!="undefined"){
params.error(xmlhttp.status+xmlhttp.statusText);
}
console.error(url+":"+xmlhttp.status);
}
}
};
}
xmlhttp.send(null);
if(!async){
if(xmlhttp.readyState==4&&xmlhttp.status==200)
if(typeofparams.success!="undefined"){
params.success(xmlhttp.responseText);
}
else{
if(typeofparams.error!="undefined"){
params.error(xmlhttp.status+xmlhttp.statusText);
}
console.error(url+":"+xmlhttp.status);
}
}
}
},
createXMLHttpRequest:function(){
if(window.XMLHttpRequest)
{
returnnewXMLHttpRequest();
}
elseif(window.ActiveXObject)
{
//codeforIE5andIE6
returnnewActiveXObject("Microsoft.XMLHTTP");
}
returnnull;
},
getJSONP:function(params){
varurl=null;
if(typeofparams.url!="undefined"){
url=params.url;
}
if(url==null){
return;
}
varff=""+newDate().getTime()+(parseInt(Math.random()*10000000000));
eval("jsonpCallback_"+ff+"="+function(data){
if(typeofparams.success!="undefined"){
params.success(data);
}
});
//根据url中是否出现过"?"来决定添加时间戳参数时使用"?"还是"&"
varparamPrefix=url.indexOf("?")==-1?"?":"&";
url=url+paramPrefix+"jsonpCallback="+"jsonpCallback_"+ff;
varparam="";
if(typeofparams.data!="undefined"&¶ms.data!=null){
vardata=params.data;
for(varpropindata){
param+=prop+"="+data[prop]+"&";
}
param=param.substring(0,param.length-1);
}
if(param.length>0)
url=url+"&"+param;
varscript=document.createElement("script");
document.body.appendChild(script);
script.src=url;
script.charset="UTF-8";
//forfirefox,googleetc.
script.onerror=function(){
if(typeofparams.error!="undefined"){
params.error();
}
}
script.onload=function(){
document.body.removeChild(script);
}
//forie
script.onreadystatechange=function(){
if(this.readyState=="loaded"||this.readyState=="complete"){
document.body.removeChild(script);
}
}
}
};
以上这篇用JS写的一个Ajax库(实例代码)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。