<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<script type = "text/javascript">
var $ = {
get: function(url,args){
var xhr = this.createXhr();
xhr.onreadystatechange = function(){
if(xhr.readyState == 4 && xhr.status == 200){
alert(xhr.responseText);
}
}
if(typeof args == 'object'){
args = this.parmas(args);
}
url = url + '?' + args;
xhr.open('get',url);
xhr.send();
},//
createXhr:function(){
var xmlhttp = '';
if(window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
console.log("hello ");
} else{
xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');
}
return xmlhttp;
},
parmas: function(js){
var str = '';
for(var key in js){
str += key + '=' + js[key] + '&';
}
str = str.slice(0,str.length-1);
return str;
}
};
$.get('1.php',{id:1001,name:"冰箱"});
</script>
</body>
</html>
if(typeof args == 'object') 对args的数据类型检测方式