cakephp中的JSON响应没有显示任何内容

时间:2014-03-25 09:54:56

标签: jquery json cakephp each

我有一个控制器api_show_employees(),如下所示:

function api_show_employees() {
      $this->autoRender = false;
      Configure::write("debug",0);
      $office_id = '';
      $cond = '';
      if(isset($_GET['office_id']) && $_GET['office_id'] != '') {
        $office_id = $_GET['office_id'];
        $cond['EmployeePersonal.office_id'] = $office_id;
      }

      $order = 'EmployeePersonal.name';
      $employee = $this->EmployeePersonal->find('all',array('order' => $order,'conditions'=>$cond));
      return json_encode($employee);
    }

我调用ajax的ctp文件是:

function populate_list(office_id) {

  var data = '';
  data = 'office_id='+office_id;
  var url_to_call = '';
  url_to_call = '<?php echo $this->Html->url(array("controller" => "employee_personals","action" => "api_show_employees")); ?>';

  $.ajax({
    type : "GET",
    url : url_to_call,
    data : data,
    dataType : "json",
    error: function (resp){
      alert('Something went wrong :(');
    },
    success: function(resp) {
     alert(resp);      
     //render_ui(resp);
    }
  });

}

当我在浏览器中检查时,我收到的回复是:

[
    {
        "EmployeePersonal": {
            "id": "88",
            "epf_no": "1093",
            "name": "DAVID DAWSON",
            "date_of_birth": "1968-12-08",
            "category": "General",
            "father_name": "N J PAUL DAWSON",
            "mother_name": "LT KARUNA DAWSON",
            "present_address": "BHAULAGURI, PO NEW BONGAIGAON",
            "present_district_id": "2",
            "permanent_address": "BHAULAGURI",
            "permanent_district_id": "2",
            "identification_mark": "BLACK MOLE ON THE CHIN",
            "blood_group": "AB+",
            "maritial_status": "married",
            "date_of_marriage": "2004-07-01",
            "date_of_joining": "1991-10-30",
            "gender": "male",
            "father_annual_income": "120000",
            "mother_annual_income": "0",
            "present_city": "BONGAIGAON",
            "permanent_city": "BONGAIGAON",
            "office_id": "2",
            "department_id": "0"
        },
        "present_district": {
            "id": "2",
            "name": "bongaigaon"
        },
        "permanent_district": {
            "id": "2",
            "name": "bongaigaon"
        },
        "office": {
            "id": "2",
            "name": "AMGURI BAZAR BRANCH",
            "address": "PO AMGURI BAZAR",
            "district_id": "27",
            "reporting_office_id": "8",
            "office_type_id": "3"
        },
        "department": {
            "id": null,
            "name": null
        },
        "EmployeePosting": [
            {
                "id": "88",
                "office_id": "2",
                "posting_from": "1991-10-30",
                "posting_to": "0000-00-00",
                "designation_id": "8",
                "employee_personal_id": "88",
                "department_id": "0"
            }
        ],
        "Office": [
            {
                "id": "2",
                "name": "AMGURI BAZAR BRANCH",
                "address": "PO AMGURI BAZAR",
                "district_id": "27",
                "reporting_office_id": "8",
                "office_type_id": "3",
                "EmployeePosting": {
                    "id": "88",
                    "office_id": "2",
                    "posting_from": "1991-10-30",
                    "posting_to": "0000-00-00",
                    "designation_id": "8",
                    "employee_personal_id": "88",
                    "department_id": "0"
                }
            }
        ],
        "designation": [
            {
                "id": "8",
                "name": "branch manager",
                "EmployeePosting": {
                    "id": "88",
                    "office_id": "2",
                    "posting_from": "1991-10-30",
                    "posting_to": "0000-00-00",
                    "designation_id": "8",
                    "employee_personal_id": "88",
                    "department_id": "0"
                }
            }
        ]
    },
    {
        "EmployeePersonal": {
            "id": "89",
            "epf_no": "1698",
            "name": "SIDHARTHA RAY",
            "date_of_birth": "1989-11-10",
            "category": "OBC",
            "father_name": "TIRTHA KR RAY",
            "mother_name": "JALESWARI RAY",
            "present_address": "CHIPONCHILA, PO CHIPONCHILA",
            "present_district_id": "2",
            "permanent_address": "CHIPONCHILA",
            "permanent_district_id": "2",
            "identification_mark": "BLACK MOLE ON THE NECK",
            "blood_group": "A+",
            "maritial_status": "single",
            "date_of_marriage": "0000-00-00",
            "date_of_joining": "2013-07-13",
            "gender": "male",
            "father_annual_income": "100000",
            "mother_annual_income": "0",
            "present_city": "BONGAIGAON",
            "permanent_city": "BONGAIGAON",
            "office_id": "2",
            "department_id": "0"
        },
        "present_district": {
            "id": "2",
            "name": "bongaigaon"
        },
        "permanent_district": {
            "id": "2",
            "name": "bongaigaon"
        },
        "office": {
            "id": "2",
            "name": "AMGURI BAZAR BRANCH",
            "address": "PO AMGURI BAZAR",
            "district_id": "27",
            "reporting_office_id": "8",
            "office_type_id": "3"
        },
        "department": {
            "id": null,
            "name": null
        },
        "EmployeePosting": [
            {
                "id": "89",
                "office_id": "2",
                "posting_from": "2013-07-13",
                "posting_to": "0000-00-00",
                "designation_id": "5",
                "employee_personal_id": "89",
                "department_id": "0"
            }
        ],
        "Office": [
            {
                "id": "2",
                "name": "AMGURI BAZAR BRANCH",
                "address": "PO AMGURI BAZAR",
                "district_id": "27",
                "reporting_office_id": "8",
                "office_type_id": "3",
                "EmployeePosting": {
                    "id": "89",
                    "office_id": "2",
                    "posting_from": "2013-07-13",
                    "posting_to": "0000-00-00",
                    "designation_id": "5",
                    "employee_personal_id": "89",
                    "department_id": "0"
                }
            }
        ],
        "designation": [
            {
                "id": "5",
                "name": "Office Assistant (M)",
                "EmployeePosting": {
                    "id": "89",
                    "office_id": "2",
                    "posting_from": "2013-07-13",
                    "posting_to": "0000-00-00",
                    "designation_id": "5",
                    "employee_personal_id": "89",
                    "department_id": "0"
                }
            }
        ]
    }
]

我在没有ajax jsfiddle的情况下进行了检查,我收到的警报为[object Object],[object Object]。

1 个答案:

答案 0 :(得分:0)

你在json响应中有一个数组,所以首先在响应中应用循环,然后获取值,如下例所示:

$.ajax({
    type : "GET",
    url : url_to_call,
    data : data,
    dataType : "json",
    error: function (resp){
      alert('Something went wrong :(');
    },
    success: function(resp) {
    for (var i=0; i<resp.length; i++) {
     alert(resp[i].EmployeePersonal.id); // you will get id
     alert(resp[i].present_district.name); // you will get district name    
    // so on...
     }
    }
  });