【问题标题】:Handle Ajax request and response zend framework处理 Ajax 请求和响应 zend 框架
【发布时间】:2012-03-06 11:20:08
【问题描述】:

我想向控制器发送 Ajax 请求,我在客户端这样做

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

当我调用该操作时,会出现失败警报,当我通过 fire bug 检查它时,我发现它会将 json 数据返回到客户端到页面 get-visit.phtml。

如何处理来自发送 json 请求的页面的成功函数中的响应并将其重定向到 get-visit.phtml 页面?

【问题讨论】:

    标签: ajax json zend-framework


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

        //客户端

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

        【讨论】:

          【解决方案4】:

          为了更正确的方式来做这件事。我会在你的控制器中使用以下内容

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

          【讨论】:

            【解决方案5】:

            您可能需要为您的操作禁用视图呈现,以防它被POST HTTP 方法调用。这是我的做法:

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

            不过,还有其他方法可以做到这一点。更多信息可以看官方ViewRenderer documentation

            希望对您有所帮助。

            【讨论】:

              【解决方案6】:

              您可以使用 JsonModel - 只需返回:

              return new JsonModel();

              别忘了添加

              使用 Zend\View\Model\JsonModel;

              【讨论】:

                【解决方案7】:
                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 :("");
                    }
                });
                

                【讨论】:

                  猜你喜欢
                  • 2015-04-26
                  • 1970-01-01
                  • 2011-02-23
                  • 1970-01-01
                  • 2021-12-02
                  • 2015-04-14
                  • 1970-01-01
                  • 1970-01-01
                  • 2012-08-17
                  相关资源
                  最近更新 更多