我正在使用Ajax将服务器端代码PHP中的一些数据发回给我的客户端,这是如何做到的
//server side
$json='{
"payout_history":"0",
"round_shares":"1816",
"workers":
{
"jbo.5970":
{
"alive":"1",
"hashrate":"1253"
},
"jbo.5970cpu":
{
"alive":"1",
"hashrate":"21"
},
"jbo.5970-2":
{
"alive":"1",
"hashrate":"1062"
}
}
}';
echo json_encode($json);这是基于firebug的JSON响应
"{\r\n\"payout_history\":\"0\",\r\n\"round_shares\":\"1816\",\r\n\"workers\":\r\n
{\r\n \"jbo.5970\":\r\n {\r\n \"alive\":\"1\",\r\n \"hashrate
\":\"1253\"\r\n },\r\n \"jbo.5970cpu\":\r\n {\r\n \"alive\":\"1
\",\r\n \"hashrate\":\"21\"\r\n },\r\n \"jbo.5970-2\":\r\n
{\r\n \"alive\":\"1\",\r\n \"hashrate\":\"1062\"\r\n }\r\n }\r\n}"在客户端,我试图使用$.each函数对每个工作人员进行迭代,以获得"jbo.5970“、”活着“、"hashrate”。我该怎么做呢?
我试过了,但是什么也没有发生,调试器中没有错误。
//client side
$.ajax({
type: "POST",
url: "display.php",
data:{faculties:"arts"},
dataType: "json", //expect json to be returned
success: function(response){
$.each(response,function(i,item)
{
alert(response["workers"]);
});
}
});发布于 2013-09-10 02:24:36
response.workers是数组,而不是response。
$.each(response.workers,function(i,item)
{
console.log(item);
});而且服务器端已经有了json字符串,所以不需要对其进行编码。
使用echo $json;而不是echo json_encode($json);。
https://stackoverflow.com/questions/18709791
复制相似问题