2015-03-25 55 views
0

我試過了一個簡單的使用jquery的ajax應用,並且錯誤是每個數據對象都是null。誰能幫我 ? 這裏的mycode的:如何在Zend Framework中使用ajax和json編碼2

protected $mysqlAdapter; 
protected $studentid; 
protected $fullname; 
protected $birthdate; 
protected $placebirth; 
protected $gender; 
protected $id_religion; 
protected $nm_religion; 
protected $stdaddr; 
protected $stdphone; 
protected $stdcellular; 
protected $stdemail; 

public function indexAction() 
{ 
    $request = $this->getRequest(); 
    $response = $this->getResponse(); 

    $nm_pd = $this->request->getPost('nm_pd'); 
    $tgl_lahir = $this->request->getPost('tgl_lahir'); 
    $id_agama = $this->request->getPost('id_agama'); 
    $nm_agama = $this->request->getPost('nm_agama'); 

    $dbAdapter = $this->getMysqlAdapter(); 
    $sql = new Sql($dbAdapter); 
    $all = $sql->select(); 
    $all->from('v_datadetail_feeder'); 
    $all->where(array('studentid' => 11509002)); 
    $statement = $sql->prepareStatementForSqlObject($all); 
    $result = $statement->execute(); 

    foreach ($result as $rs) { 
     $studentid = $this->$rs['studentid']; 
     $fullname = $this->$rs['fullname']; 
     $birthdate = $this->$rs['birthdate']; 
     $placebirth = $this->$rs['placebirth']; 
     $gender = $this->$rs['gender']; 
     $id_religion = $id_agama; 
     $nm_religion = $nm_agama; 
     $stdaddr = $this->$rs['stdaddr']; 
     $stdphone = $this->$rs['stdphone']; 
     $stdcellular = $this->$rs['stdcellular']; 
     $stdemail = $this->$rs['stdemail']; 
     $data = array('fullname'=>$fullname, 
         'studentid'=>$studentid, 
         'birthdate'=>$birthdate, 
         'placebirth'=>$placebirth, 
         'gender'=>$gender, 
         'id_agama'=>$id_religion, 
         'nm_agama'=>$nm_religion, 
         'stdaddr'=>$stdaddr, 
         'stdphone'=>$stdphone, 
         'stdcellular'=>$stdcellular, 
         'stdemail'=>$stdemail, 
        ); 
     $response->setContent(Json::encode($data));   
    } 
    return $response; 
} 

而結果: { 「全名」:空, 「studentid」:空, 「出生日期」:空, 「placebirth」:空, 「性別」:空, 「id_agama」 :空, 「nm_agama」:空, 「stdaddr」:空, 「stdphone」:空, 「stdcellular」:空, 「stdemail」:空}

enter image description here

回答

3

你必須使用ZF2 JsonModel() ,在module.config.php文件中啓用JsonStrategy

'view_manager' => array (
     'strategies' => array (
      'ViewJsonStrategy' 
     ) 
), 

,並用它你這樣的控制器內部:

$view = new JsonModel($jsonArray); 
$view->setTerminal(true); // to disable layout 
return $view; 
+0

如果我使用setTerminal()這將是致命的錯誤 – 2015-03-25 07:49:35

+0

@FebryDamatrasetaFairuz什麼錯誤? – Vipul 2015-03-25 08:48:39

相關問題