2014-03-25 3 views
0

У меня есть контроллер api_show_employees(), как показано ниже:JSON ответ в CakePHP не показывая ничего

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

И файл 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, я m получать предупреждения как [object Object], [object Object].

+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... 
    } 
    } 
    }); 
Смежные вопросы