我的 PHP 脚本
<?php
//connect to server
//selecting the database
$temparr=array();
$count=0
$result = mysql_query("some query");
while($row = mysql_fetch_assoc($result))
{
$temparr["$count"]= $row["value"] ;
$count+=1;
}
echo json_encode($temparr);
mysql_close($conn);
?>
我的javascript文件中的AJAX函数调用如下
function someFunction(){
xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "mymethodpath", true);
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
alert(xmlhttp.responseText);
//this gives me a popup of the encoded data returned by the php script
// the format i see in the popup window is ["1","2"]
var temp=$.parseJSON(xmlhttp.responseText);
alert(temp.count);
//however this produces an undefined value
}
xmlhttp.send();
}
那么我如何解析返回的字符串并显示正确的计数(这里计数应该是 2)