Yii Framework Forum: Access Properties Of Ajax Returned Object - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Access Properties Of Ajax Returned Object Rate Topic: -----

#1 User is offline   yii_king 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 110
  • Joined: 05-August 12

Posted 07 October 2012 - 07:14 PM

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.

Quote

$sql = "SELECT * FROM tbl_p";
$p = ForecastManager::model()->findAllBySql($sql);
if ($p!=null){
print_r(json_encode(array('p'=>$p)));
return;
}
else {
print_r(json_encode("null"));
return;
}


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

Quote

$.ajax({
type: 'POST',
url: '<?php echo Yii::app()->createAbsoluteUrl("f/s"); ?>',
data:data,
success:function(data){
alert(data);
},
error: function(data) { // if error occured
alert("Error occured.please try again");
alert(data);
},

dataType:'html'
});



So the javascript alert is then printing out this:

Quote

{"p":[{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}]}


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

#2 User is offline   waterloomatt 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 545
  • Joined: 09-April 10

Posted 07 October 2012 - 10:09 PM

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

$attributes = array();

foreach ($forecasts as $forecast)
{
    $attributes[] = $forecast->getAttributes();
}

echo json_encode($attributes);


Matt
0

#3 User is offline   bennouna 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,166
  • Joined: 05-January 12
  • Location:Morocco

Posted 08 October 2012 - 01:51 AM

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

#4 User is offline   yii_king 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 110
  • Joined: 05-August 12

Posted 08 October 2012 - 03:57 AM

Thank you waterloomatt and bennouna for your answers.

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

Quote

[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]


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

Quote

// Prints 0, 1 etc. to 18
for (key in data) {
alert(data[key]);
}
// Unitname is a field retrieved from db. Print out same as above
for (unitName in data) {
alert(data[unitName]);
}



Any ideas about this?
0

#5 User is offline   yii_king 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 110
  • Joined: 05-August 12

Posted 08 October 2012 - 04:09 AM

View Postyii_king, on 08 October 2012 - 03:57 AM, said:

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:

Quote

// Note that id should equal the property that you want to access
$.each(data, function(i, val) {
alert(val.id);
});


Many thanks again for your help.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users