<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<script>
function myAjax(type,url,data,func){
if(window.XMLHttpRequest){
var xhr=new XMLHttpRequest();
}else{
var xhr=new ActiveXObject('Microsoft.XMLHTTP');
}
var str='';
// 数据是对象形式
for(var i in data ){
str+='&'+i+'='+data[i];
}
// 获取到的字符串以&开头
// 判断类型,怎么拼接字符串,然后发送
if(type.toLowerCase().trim()=='get'){
var url_index=url.indexOf('?');
if(url_index==-1){
url+='?'+str.substring(1);//获取到要拼接的字符串
}else if(url_index==(url.length-1)){
// 有?,若以?结尾
url+=str+str.substring(1);
}else{
url+=str;
}
xhr.open(type,url);
xhr.send();
}else{
xhr.open(type,url);
xhr.setRequestHeader('content-type','application/x-www-form-urlencoded');
xhr.send(str.substring(1));
}
xhr.onreadystatechange= function () {
if(xhr.readyState==4&&xhr.status>=200&&xhr.status<=207||xhr.status==304){
func(json_data);
}
};
}
</script>
</body>
</html>