function ajax(d){
//默认同源请求
var dataType= d.dataType=="jsonp"?"jsonp":((d.dataType=="json"|| d.dataType.trim())==""?"json":"f");
//默认get请求
var type= d.type=="post"?"post":((d.type.trim()==""|| d.type=="get")?"get":"f");
var url= d.url+"?";
for(var key in d.data){
url+=key+"="+d.data[key]+"&";
console.log(d.data[key]);
}
//将最后一个?符或者&符去掉
url=url.slice(0,url.length-1);
//当请求方式输入错误时,返回函数
if(dataType=="f"||type=="f"){
return;
}
if(dataType=="json"){
var xhr;
if(XMLHttpRequest){
xhr=new XMLHttpRequest();
}else{
xhr=new ActiveXObject("Microsoft.XMLHTTP");
}
if(type=="post"){
xhr.open("post",url.split("?")[0]);
xhr.setRequestHeader("Content-type","application/X-WWW-form-urlencoded");
xhr.send(url.split("?")[1]);
}else{
xhr.open("get",url);
xhr.send(null);
}
xhr.onreadystatechange==function(){
if (xhr.readyState == 4 && xhr.status == 200) {
d.success(xhr.responseText);
}
}
//};
}else{
var cbname="ajax"+new Date().getTime()+Math.random().toString().slice(2);
window[cbname]=function(dd){
d.success(dd);
document.removeChild(script);
}
var script=document.createElement("script");
script.src=url+"&callback="+cbname;
document.body.appendChild(script);
}
}