doutizong8099
doutizong8099
2018-11-07 09:02

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

  • oop
  • php
  • ajax
  • jquery

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条回答