处理Ajax请求和响应Zend框架

问题描述:

我要发送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页面的页面处理成功函数中的响应?

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

如果使用HTTP方法调用POST,您可能需要禁用视图渲染。下面是我如何做到这一点:

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

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

希望有所帮助。

//客户端

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; 

    } 

这样做的一个比较正确的做法。我会用下面的控制器

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

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 :(""); 
    } 
}); 

在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; 
    ... 
}, 

您可以使用JsonModel - 简单的返回:

return new JsonModel();

不要忘记添加

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