【问题标题】:Stop layout rendering for Ajax request with custom routing使用自定义路由停止 Ajax 请求的布局呈现
【发布时间】:2012-05-20 10:41:01
【问题描述】:

如何在通过 AJAX 发送请求时停止布局呈现。 我面临的问题是 json 数据在浏览器上回显,而不是传递给 jquery 的回调函数

这是我的脚本

jQuery.ajax({
    url: "/getPrivileges",
    type: "POST",
    dataType: 'JSON',
    success: function(privileges){
        alert('hellooo');

        buildTree(privileges,".privBox h2");
        if($(".privPrivilege"))
            $("#loading").css("visibility","hidden");
    },
    error: function (request, status, error) {
        alert('Error'+request.responseText);
    }

});

这是路由

resources.router.routes.privilege.route = /getPrivileges
resources.router.routes.privilege.defaults.module = privileges
resources.router.routes.privilege.defaults.controller = privilege
resources.router.routes.privilege.defaults.action = get-privileges

这是我的控制器

  public function getPrivilegesAction() {

        if ($this->getRequest()->isXmlHttpRequest()) {
         ...........
         ...........
          $this->_helper->json($appPrivArray);
          $this->_helper->viewRenderer->setNoRender(TRUE);
          $this->_helper->layout->disableLayout();
          $response = $this->getResponse();
          $response->setHeader('Content-type', 'application/json', true);
   }


 }

首先我面临的布局仍然呈现,但现在 json 打印在屏幕上,即使我没有 get-privileges.phtml 视图页面。

在控制器的 init() 方法中,我是这样的

 public function init() {
    $ajaxContextSwitch = Zend_Controller_Action_HelperBroker::getStaticHelper('AjaxContext');
    $ajaxContextSwitch->setDefaultContext('json');
    $ajaxContextSwitch->addActionContext('getPrivileges', 'json');
    $ajaxContextSwitch->initContext();
}

如何使响应传递到 jquery 的回调函数! 值得一提的是,并非所有 url 都有自定义路由。

请帮忙,因为我几乎要退出使用 Zend 框架的开发了!!

【问题讨论】:

    标签: php ajax json zend-framework layout


    【解决方案1】:

    在动作中,你只需要使用

    $this->_helper->json($appPrivArray)
    

    您不需要使用setNoRender 或其他任何东西。 在 Javascript 中,您不需要使用 dataType: 'JSON', 我测试了这个,应该可以正常工作。

    【讨论】:

    • 我把所有的cmets都放在draw010 answer上。请检查一下。
    • 如果您仍在获取布局,则有一种可能性是操作中存在错误,并且布局显示异常消息和堆栈跟踪,而不是仅返回 json。有没有检查返回的代码,看看里面有没有异常信息?
    • 我遇到的问题是路由,我有两条同名的路由,所以这就是问题..
    • 好的。一个好主意是将动作控制器代码包装在try ... catch 中,并在发生错误时将异常消息作为 json 返回。喜欢catch ( Exception $e ) { return $this->_helper->json( array('error' => $e->getMessage()) ) }
    • 嗯,我通常返回 $e 对象,谢谢你的 getMessage() 信息:)
    【解决方案2】:

    JSON 数据通过“在浏览器上回显”返回给 jQuery(或任何其他库)。这就是回调接收数据的方式。

    JSON 视图助手会为您禁用布局,并将 Content-Type 标头设置为 application/json,因此您不必这样做。

    如果您没有给定操作的视图脚本,您可以继续使用setNoRender(true)。

    我认为这段代码应该可以正常工作:

    public function getPrivilegesAction() {
        if ($this->getRequest()->isXmlHttpRequest()) {
            $this->_helper->viewRenderer->setNoRender(TRUE);
            $this->_helper->json($appPrivArray);
        }
    }
    

    您是否收到任何 Javascript 错误或 alert('hello') 没有运行?

    【讨论】:

    • 现在js出现错误提示这个还有整个布局的html!
    • @Salman,我按照你说的做了,现在它提醒你好,胸围仍然提醒布局脚本!
    • 我发出警报('hello'+privilege);它提醒布局页面
    猜你喜欢
    • 2017-05-02
    • 2013-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多