开发者

return a php associative array to javascript array

I am trying to return a php associative array to javascript array through ajaxRequest.responseText

Here's what I do.

First in php, I do this:

$encoded = json_encode($thisarray);
echo $encoded; 

If I echo $encoded, I get {"a":"apple,arrow","b":"boy,bank","c":"cat,camp"}

Then in js script,

thisarray = new Array();
thisarray = ajaxRequest.responseText;

If I alert thisarray, I get {"a":"apple,arrow","b":"boy,bank","c":"cat,camp"}

That's wrong since alerting an array should give error. But in this case, when I alert thisarray, I get the full array!!

Needles开发者_JAVA百科s to say, I can't call my value out of thisarray, since it is yet defined as an array.

Anyone can tell me what am I missing here?


You need to parse the JSON string in JavaScript to get an object, preferably with the native JSON object of your browser, if available:

var thisarray = JSON.parse(ajaxRequest.responseText);

Otherwise you can use the JSON parser from JSON.org or jQuery.parseJSON if you’re already using jQuery.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜