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
.
精彩评论