请问! 获取JSON返回数据输出有关问题
请教! 获取JSON返回数据输出问题
<script type="text/javascript">
<!--
var xmlhttp;
// 创建XMLHTTPRequest对象
function createXMLHTTPRequest()
{
if(window.ActiveXObject)//②如果当前浏览器为IE
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHTTPRequest)//③如果是其他浏览器
{
xmlhttp = new XMLHTTPRequest();
}
}
function getInfo()
{
var bh =document.getElementById("bh").value;
var name=document.getElementById("name").value;
createXMLHTTPRequest();
if ((bh == null) || (bh == "")) return;
var url = "1.asp?bh=" + escape(bh);
url=url + "&name=" + name;
xmlhttp.open("GET", url, true);
xmlhttp.onreadystatechange = returnInfo;
xmlhttp.send(null);
}
function returnInfo()
{
if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
var info = xmlhttp.responseText;
// shownr.innerHTML = info;
//返回 info = {"traderates_get_response":{"trade_rates":{"trade_rate":[{"content":"很好!","nick":"平地","result":"good","valid_score":false},{"content":"不错!","nick":"平地","result":"good","valid_score":true}]},"total_results":2}}
showscore.innerHTML = valid_score;
shownick.innerHTML = nick;
showcontent.innerHTML = content;
showresult.innerHTML = result;
}
}
-->
</script>
<input name="bh" type="hidden" id="bh" >
<input name="name" type="hidden" id="name" >
<body onLoad="getInfo()">
页面输出,如:
<table width="100%" border="0" cellspacing="0" cellpadding="5">
<tr>
<td>nick</td>
<td>valid_score</td>
<td>result</td>
<td>content</td>
</tr>
<tr>
<td><span id="shownick"></span>
</td>
<td><span id="showscore"></span>
</td>
<td><span id="showresult"></span>
</td>
<td><span id="showcontent"></span>
</td>
</tr>
</table>
</body>
</html>
------解决思路----------------------
下载 json2.js
https://github.com/douglascrockford/JSON-js
然后
var info = JSON.parse(xmlhttp.responseText);
就可以了
<script type="text/javascript">
<!--
var xmlhttp;
// 创建XMLHTTPRequest对象
function createXMLHTTPRequest()
{
if(window.ActiveXObject)//②如果当前浏览器为IE
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHTTPRequest)//③如果是其他浏览器
{
xmlhttp = new XMLHTTPRequest();
}
}
function getInfo()
{
var bh =document.getElementById("bh").value;
var name=document.getElementById("name").value;
createXMLHTTPRequest();
if ((bh == null) || (bh == "")) return;
var url = "1.asp?bh=" + escape(bh);
url=url + "&name=" + name;
xmlhttp.open("GET", url, true);
xmlhttp.onreadystatechange = returnInfo;
xmlhttp.send(null);
}
function returnInfo()
{
if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
var info = xmlhttp.responseText;
// shownr.innerHTML = info;
//返回 info = {"traderates_get_response":{"trade_rates":{"trade_rate":[{"content":"很好!","nick":"平地","result":"good","valid_score":false},{"content":"不错!","nick":"平地","result":"good","valid_score":true}]},"total_results":2}}
showscore.innerHTML = valid_score;
shownick.innerHTML = nick;
showcontent.innerHTML = content;
showresult.innerHTML = result;
}
}
-->
</script>
<input name="bh" type="hidden" id="bh" >
<input name="name" type="hidden" id="name" >
<body onLoad="getInfo()">
页面输出,如:
<table width="100%" border="0" cellspacing="0" cellpadding="5">
<tr>
<td>nick</td>
<td>valid_score</td>
<td>result</td>
<td>content</td>
</tr>
<tr>
<td><span id="shownick"></span>
</td>
<td><span id="showscore"></span>
</td>
<td><span id="showresult"></span>
</td>
<td><span id="showcontent"></span>
</td>
</tr>
</table>
</body>
</html>
------解决思路----------------------
下载 json2.js
https://github.com/douglascrockford/JSON-js
然后
var info = JSON.parse(xmlhttp.responseText);
就可以了