2012-03-19 227 views
0

当我尝试从fetchAll返回json时,我注意到一个错误。json编码数组返回空的json对象,如何?

控制器

我有一个fetchAction

$result = $model->fetchAll(); 
echo Zend_Json::encode($result); 
exit(); 

在.js文件中我有一个Ajax调用:

$.ajax({ 
    type: "GET", 
    url: "/module/index/fetch", 
    dataType: "json", 
    processData: false, 
    success: function(data) { 
     console.log(data); 
     $.each(data, function(index, obj){ 
      // console.log(obj); 
     }); 
    } 
}); 

,如果我做一个print_r($result);我得到:

Array 
(
    [0] => Module_Model_Module Object 
     (
      [_message:protected] => test 
      [_created:protected] => 1332188757 
      [_dependent_table:protected] => 
     ) 

    [1] => Module_Model_Module Object 
     (
      [_message:protected] => 123123 
      [_created:protected] => 1332194812 
      [_dependent_table:protected] => 
     ) 

) 

,但console.log(data);返回2个空json对象:[{},{}]

任何想法,为什么我得到的json空回来?

回答

0

我的猜测是,Zend_Json不能改造Module_Model_Module对象成阵列,然后到JSON,你可以尝试指定者打电话的对象,看看是否再Zend_Json能正确编码他们