doutizong8099 2018-11-07 09:02
浏览 84
已采纳

Ajax调用返回一个空对象数组

Am using OOP of php,am fetching data from the database. data is fetched well but when i make ajax calls , it returns an array of empty objects. how can to make objects return the data

fetched database results before json_encoding it

Array ( [0] => CategoryModel Object ( [id:CategoryModel:private] => 1 [code:CategoryModel:private] => cat001 [name:CategoryModel:private] => laptop [date:CategoryModel:private] => 2018-11-05 15:33:21 [owner:CategoryModel:private] => OwnerModel Object ( [companyName:OwnerModel:private] => [id:PersonModel:private] => 1 [code:PersonModel:private] => [firstname:PersonModel:private] => [lastname:PersonModel:private] => [username:PersonModel:private] => [email:PersonModel:private] => [password:PersonModel:private] => [date:PersonModel:private] => [mobile:PersonModel:private] => [fullname:PersonModel:private] => Galiwango Fahad [account_type:PersonModel:private] => ) [status:CategoryModel:private] => 0 ) [1] => CategoryModel Object ( [id:CategoryModel:private] => 2 [code:CategoryModel:private] => cat002 [name:CategoryModel:private] => pen [date:CategoryModel:private] => 2018-11-05 16:05:29 [owner:CategoryModel:private] => OwnerModel Object ( [companyName:OwnerModel:private] => [id:PersonModel:private] => 1 [code:PersonModel:private] => [firstname:PersonModel:private] => [lastname:PersonModel:private] => [username:PersonModel:private] => [email:PersonModel:private] => [password:PersonModel:private] => [date:PersonModel:private] => [mobile:PersonModel:private] => [fullname:PersonModel:private] => Galiwango Fahad [account_type:PersonModel:private] => ) [status:CategoryModel:private] => 0 ) )

php code that retrieves and encodes the data

<?php

require_once("../services/CategoryService.php");
$categoryService = new CategoryService();
$categoryArray =  $categoryService->selectAllCategoryDetails();
if(count($categoryArray)>0){
// echo print_r($categoryArray);
  echo json_encode($categoryArray);


}else{
  echo 0;
}

?>

javascript

$.ajax({
    type: "POST",
    url: ".././com.flex_audit/controllers/fetchAllCategoriesController.php",
    data: "",

    success: function (responseData) {
        if(responseData!=0){
            var categoryObjArr = JSON.parse(responseData);
            console.log(categoryObjArr);
            console.log(categoryObjArr[0]);
            console.log(categoryObjArr[0]["id"]);
        }else{
            $("#alertMessage").text(" categories not found ");
        }
       // console.log(responseData);    
    }
    ,
    error:function(error){
      alert("an error occured "+error);
    }
});

  });

console result

(2) [{…}, {…}]
 {}
 undefined

why is that the response returns empty objects

  • 写回答

1条回答 默认 最新

  • doubei8168 2018-11-07 09:34
    关注

    Because all properties of the objects are private *it worked after making class properties public instead of private *

    from

    private $id,$code,$name,$date,$owner,$status;
    

    to

    public $id,$code,$name,$date,$owner,$status;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Linux环境下CA证书更新问题
  • ¥60 微信小程序如何上传QQ聊天文件
  • ¥300 开发的系统遭到无良商家的破解,请问如何防止再次发生,并追回损失
  • ¥15 java代码写在记事本上后在cmd上运行时无报错但又没生成文件
  • ¥15 关于#python#的问题:在跑ldsc数据整理的时候一直抱这种错误,要么--out识别不了参数,要么--merge-alleles识别不了参数(操作系统-linux)
  • ¥15 PPOCRLabel
  • ¥15 混合键合键合机对准标识
  • ¥100 现在不懂的是如何将当前的相机中的照片,作为纹理贴图,映射到扫描出的模型上
  • ¥15 安卓OpenCV人脸识别分类器加载
  • ¥15 魔霸ROG7 pro,win11.息屏后会显示黑屏,如图,如何解决?(关键词-重新启动)