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

446

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(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...

765

Answer

Solution:

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. tryalert(data.key) and you should getvalue.

People are also looking for solutions to the problem: php - CakePHP HATBM not working

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.