开发者

jquery ajax get json array?

i use $.getJSON to call php file. it echos this back.

for($i=0; $i<=10; $i++)
{
    $nr[] = $i;
}

echo "{'result': 'error', 'count': '$nr'}";

in jquery to get result i just use

alert(data.result)

how do i get all the nr:s in count?

EDIT: But how do i loop the array through in jquery? All the keys are numeric and i dont know how to retrieve开发者_如何学Go them.


Use json_encode to convert your PHP array into an JSON array:

echo "{'result': 'error', 'count': ".json_encode($nr)."}";

You can even build your hole response message with native PHP types first and then convert it with json_encode:

$response = array(
    'result' => 'error',
    'count'  => array()
);
for ($i=0; $i<=10; $i++) {
    $response['count'][] = $i;
}
echo json_encode($response);


... and you can loop through the values in jQuery with the .each() method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜