2014-03-25 112 views
0

我有如下控制器api_show_employees()JSON响应不显示任何

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,我a将警报作为[对象对象],[对象对象]。

+0

它是一个对象。这可以使用'console.log(resp);'查看它 – cornelb

回答

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... 
    } 
    } 
    });