没有响应来自AJAX请求Zend框架

时间:2015-11-25 06:23:36

标签: ajax zend-framework

  

这是我的控制器:

    public function listAction()
{
    $this->_flashMessenger = $this->_helper->getHelper('FlashMessenger');
    $this->view->messages = $this->_flashMessenger->getMessages();

    $this->view->targetUrl = BASE_URL . "beheer/page/list/populate";
}
  

并且get-data操作正在跟随

public function populateAction()
{
    $this->_helper->viewRenderer->setNoRender(true);
    $this->view->layout()->disableLayout();

    $request = $this->getRequest()->getPost();
    $message = [$request['message']];

    $data = ['ok'];
    echo $this->_helper->json->sendJson($data);
    exit;
}
  

这是我的列表视图:

<script type="text/javascript">
    $(document).ready(function() {
        $("#submit").click(function() {
        var message = $('#message').val();
        $.ajax({
            url: "<?php echo $this->targetUrl ?>",
            contentType: "application/json;charset=utf-8",
            type: "POST",
            data: message,
            success: function (data) {

                alert(data);
                console.log(data);
            },
            error : function(error) {
                alert('error');
                console.log(error);

            }
        });

        });
    });
</script>

<div>
    <input type = "text" name="message" id="message">
    <input type="submit" name="submit" id="submit">
</div>

我已经检查了我的控制台,它显示整个html.Ajax功能看起来没问题,但我没有得到任何Json响应。我不知道为什么。请帮我。谢谢!

1 个答案:

答案 0 :(得分:0)

根据Zend文档(http://framework.zend.com/manual/1.12/en/zend.controller.actionhelpers.html),您可以使用以下任一方式发送JSON响应(从其网站复制)

// Direct helper call
$this->_helper->json($data, true, array('keepLayouts' => true);

// ...or, call a method of the helper
$this->_helper->sendJson($data, array('keepLayouts' => true));

所以你的代码应该是这样的......

public function populateAction()
{
    $this->_helper->viewRenderer->setNoRender(true);
    $this->view->layout()->disableLayout();

    $request = $this->getRequest()->getPost();
    $message = [$request['message']];

    $data = ['ok'];
    echo $this->_helper->sendJson($data); // modified this part
    exit;
}