2012-03-06 36 views
4

我要發送Ajax請求到控制器,我作出這樣的客戶端處理Ajax請求和響應Zend框架

jQuery.ajax({ 
    url: "public/visits/visit/get-visits", 
    type: "POST", 
    dataType: 'json', 
    data: data, 
    success: function(data){ 
     alert(data) 
    }, 
    error:function(){ 
     alert("fail :("); 
    } 
}); 

在服務器端我處理請求其他請求

public function getVisitsAction() { 
if (isset($_POST)) { 
    $mapper = new Visits_Model_VisitsMapper(); 
    $allVisits = $mapper->getAllVisits(); 
    echo json_encode($allVisits); 
} 

當我調用動作時,發生失敗警報,當我通過火蟲檢查出來時,發現它將json數據返回給客戶端頁面get-visit.phtml。

如何從發送json請求並將其重定向到get-visit.phtml頁面的頁面處理成功函數中的響應?

回答

17

Zend公司有Zend_Controller_Action_Helper_Json裏面做這些動作:

$this->_helper->layout()->disableLayout(); 
$this->_helper->viewRenderer->setNoRender(true); 
echo json_encode($allVisits); 
exit; 

所以它可能是更簡單:

public function getVisitsActions() { 
    if ($this->getRequest()->isXmlHttpRequest()) { 
     if ($this->getRequest()->isPost()) { 
      $mapper = new Visits_Model_VisitsMapper(); 

      $this->_helper->json($mapper->getAllVisits()); 
     } 
    } 
    else { 
     echo 'Not Ajax'; 
     // ... Do normal controller logic here (To catch non ajax calls to the script) 
    } 
} 
0

如果使用HTTP方法調用POST,您可能需要禁用視圖渲染。下面是我如何做到這一點:

Zend_Controller_Front::getInstance()->setParam('noViewRenderer', true); 

雖然有其他的方法可以做到這一點。您可以在官方ViewRenderer documentation中查看更多信息。

希望有所幫助。

2

//客戶端

jQuery.ajax({ 
    url: "public/visits/visit/get-visits", 
    type: "POST", 
    dataType: 'json', 
    data: data, 
    success: function(data){ 
     for(i=0;i<data.length;i++){ 
      alert(data[i]); 
     } 
    }, 
    error:function(){ 
     alert("fail :("); 
    } 
}); 

//服務器端

public function getVisitsAction() { 
    $this->_helper->layout()->disableLayout(); 
    $this->_helper->viewRenderer->setNoRender(true); 
    if (isset($_POST)) { 
     $mapper = new Visits_Model_VisitsMapper(); 
     $allVisits = $mapper->getAllVisits(); 
     echo json_encode($allVisits); 
     exit; 

    } 
5

這樣做的一個比較正確的做法。我會用下面的控制器

public function getVisitsActions() { 
    if ($this->getRequest()->isXmlHttpRequest()) { 
     if ($this->getRequest()-isPost()) { 

      $mapper = new Visits_Model_VisitsMapper(); 
      $allVisits = $mapper->getAllVisits(); 

      $this->_helper->layout()->disableLayout(); 
      $this->_helper->viewRenderer->setNoRender(true); 
      echo json_encode($allVisits); 
      exit; 
     } 
    } 
    else { 
     // ... Do normal controller logic here (To catch non ajax calls to the script) 
    } 
} 
+0

+1,使用AjaxContext會更好。 – Liyali 2012-03-06 14:33:54

-1
jQuery.ajax({ 
    url: "public/path to", 
    type: "POST", 
    dataType: 'json', 
    data: data, 
    success: function(data){ 
     for(i=0;i<data.length;i++){ 
      alert(data[i]); 
     } 
    }, 
    error:function(){ 
     alert("fail :(""); 
    } 
}); 
1

在Zend的同時,使用Zend json,你不需要進一步解析ajax部分的數據。 Zend自己做。 還響應頭:Content-Type:application/json

服務器端

$this->_helper->json($data); 

客戶端

jQuery.ajax({ 
    url: "public/path to", 
    type: "POST", 
    dataType: 'json', 
    data: data, 
    success: function(data){ 
     var username = data.user_name; 
    ... 
}, 
0

您可以使用JsonModel - 簡單的返回:

return new JsonModel();

不要忘記添加

使用Zend的\查看\型號\ JsonModel;