开发者

Unable to send an associative array in JSON format in Zend to client

In one of my actions in a controller, I'm using the json view helper to send back a response to an ajax request. On the client side I alert the data that is passed to the success callback function. It works fine as long as the response is a number or an array with default keys. Once I try to send an associative array, it alerts with [object Object]. Server code:

$childArray = array('key'=>'value');
$this->_helper->json($childArray);

javascript:

function displayChildren(data){
    alert(开发者_Python百科data);
}
...    
$.ajax({
        url: "/po/add", dataType: "json",
    data: {format: "json"}, success: displayChildren
});

I have no idea what am I doing wrong here, so any help would be appreciated...


That's expected. Associative arrays in Javascript are objects. Alert won't iterative over the object's properties and just outputs [object Object]. The key/value pairs you set on the PHP side are there and be accessed. try alert(data.key) and you should get value.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜