62,046
社区成员
发帖
与我相关
我的任务
分享
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>无标题页</title>
</head>
<body>
<div>
<input type="button" value="return ajax JSON's value"
onclick="startRequest();" />
<input type="text" id="txt1"/>
</div>
</body>
<script type="text/javascript">
var xmlHttp;
function createXMLHttpRequest(){
if(window.ActiveXObject){
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest){
xmlHttp = new XMLHttpRequest();
}
}
function startRequest(){
createXMLHttpRequest();
try{
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.open("GET", "DATA/Agent.aspx", true);
xmlHttp.send(null);
}catch(exception){
alert("您要访问的资源不存在!");
}
}
function handleStateChange(){
if(xmlHttp.readyState == 4){
if (xmlHttp.status == 200 || xmlHttp.status == 0){
// 取得返回字符串
var resp = xmlHttp.responseText;
// 构造返回JSON对象的方法
var func = new Function("return " + "[" + resp + "]");
// 得到JSON对象
var json = func( );
// 显示返回结果
var ss="";
document.getElementById("txt1").value = "";
for(var i=0,l=json.length;i<l;i++)
{
for(var key in json[i])
{
//alert(key+':'+json[i][key]);
ss += key+':'+json[i][key] + "\n";
}
}
document.getElementById("txt1").value = ss;
}
}
}
startRequest();
</script>
</html>