Access Properties Of Ajax Returned Object

I have created an AJAX POST request to my controller and am able to send back a hardcoded value which is printed out in the view file.

However, when I query the database and try to return a json_encoded object, it just returns empty properties. The PHP code to query the db should be correct as I am using the exact same query to load the page the first time.

The javascript used to print out the returned data looks like this:

So the javascript alert is then printing out this:

Any ideas of why it’s printing out empty values? How can I access each value returned?

Try this

$forecasts = ForecastManager::model()->findAll();

$attributes = array();

foreach ($forecasts as $forecast)


    $attributes[] = $forecast->getAttributes();


echo json_encode($attributes);


In addition to Matt’s suggestion, you should use ‘json’ in the ‘dataType’ property of your Ajax call (instead of ‘html’)

Thank you waterloomatt and bennouna for your answers.

After adjusting the code as suggested by you, it now prints out this:

I’m now trying to access the properties of each object, but not getting it working. I’ve tried with this for example:

Any ideas about this?

I’ve solved this now by using this in the view file:

Many thanks again for your help.