function GetAjax(callback){
var xmlhttp = null;
if(window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
}else{
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function(){
//当请求状态为4时,响应状态为200时
callback(xmlhttp);
}
return xmlhttp;
}
<html>
<script type="text/javascript" src="js/usefull.js"></script>
<script>
function myclick(){
var xmlhttp = GetAjax(function(obj){
if(obj.readyState==4 && obj.status==200)
{
var txt = obj.responseText;
alert(txt);
var json = eval("(" + txt +")");
alert(json[0].name + json[1].name + json[2].name +json[3].name);
}
}) ;
xmlhttp.open("GET","hello.asp",true);
xmlhttp.send();
}
</script>
<body>
<button type="button" οnclick="myclick()">点击</button>
</body>
</html>