【问题标题】:simple json response with cakephp使用 cakephp 的简单 json 响应
【发布时间】:2015-03-17 17:09:09
【问题描述】:

我尝试将一些 json 传递给 cakePHP 2.5 中的控制器并再次返回它以确保一切正常。

但是我没有收到任何回复内容。只成功了200。通过阅读文档,我的印象是,如果我传递了一些 json,那么 responseHandler 将返回 json 作为响应。

不知道我错过了什么。

正在传递的数据

var neworderSer = $(this).sortable("serialize");

给了

item[]=4&item[]=3&item[]=6&item[]=5&item[]=7 

appController.php

public $components = array(
    'DebugKit.Toolbar', 
    'Search.Prg', 
    'Session', 
    'Auth',
    'Session',
    'RequestHandler'
);

index.ctp

    $.ajax({
        url: "/btstadmin/pages/reorder",
        type: "post",
        dataType:"json",
        data: neworderSer,
        success: function(feedback) {
            notify('Reordered pages');
        },
        error: function(e) {
            notify('Reordered pages failed', {
                status: 'error'
            });
        }
    });

PagesController.php

public function reorder() {

    $this->request->onlyAllow('ajax');
    $data = $this->request->data;
    $this->autoRender = false;
    $this->set('_serialize', 'data');

}

更新: 我现在已将以下内容添加到 routes.php

    Router::parseExtensions('json', 'xml');

我已将控制器更新为

    $data = $this->request->data;

    $this->set("status", "OK");
    $this->set("message", "You are good");
    $this->set("content", $data);
    $this->set("_serialize", array("status", "message", "content"));

现在一切正常。

【问题讨论】:

  • 所以你正在使用(或者你想要)JSON view,对吧?
  • 是的,我想使用 JSON 视图,通过阅读文档,cakephp 是否没有选择所需的适当视图?

标签: ajax json cakephp


【解决方案1】:

应提供适当的Accept 标头或扩展名

为了让请求处理程序能够选择正确的视图,您需要发送适当的 Accept 标头 (application/json),或提供扩展名,在您的情况下为 .json。为了让扩展完全被识别,扩展解析需要被启用。

http://book.cakephp.org/...views.html#enabling-data-views-in-your-application

视图只序列化视图变量

JSON 视图仅自动序列化视图变量,从您展示的代码来看,您似乎从未设置过名为 data 的视图变量。

http://book.cakephp.org/...views.html#using-data-views-with-the-serialize-key

需要渲染视图

除非您有充分的理由,否则您不应禁用 auto rendering,并且在您的情况下,最后还要手动调用 Controller:render()。目前,您的操作甚至根本不会尝试渲染任何内容。

CakeRequest::onlyAllow() 用于 HTTP 方法

CakeRequest::onlyAllow()(顺便说一句是deprecated as of CakePHP 2.5)用于指定允许的HTTP方法,即GETPOSTPUT等。同时使用任何可用的检测器,例如@ 987654337@ 会起作用,你可能不应该依赖它。

长话短说

您的reorder() 方法应该看起来更像这样:

public function reorder() {
    if(!$this->request->is('ajax')) {
        throw new BadRequestException();
    }
    $this->set('data', $this->request->data);
    $this->set('_serialize', 'data');
}

最后,如果您不想/不能使用 Accept 标头,则需要将 .json 扩展附加到 AJAX 请求的 URL:

url: "/btstadmin/pages/reorder.json"

并因此在您的 routes.php 中启用扩展解析,例如:

Router::parseExtensions('json');

ps

请参阅 Cakephp REST API remove the necessity of .format,了解在不使用扩展的情况下使用 JSON 视图的方法。

【讨论】:

  • 谢谢。我有 reorder.json 但我破坏了应用程序。我错过了 Router::parseExtensions('json', 'xml');现在一切正常
【解决方案2】:

输出你的json数据

public function reorder() {

$this->request->onlyAllow('ajax');
$data = $this->request->data;
$this->autoRender = false;
$this->set('_serialize', 'data');
echo json_encode($data);

}

【讨论】:

  • 是的,我刚刚尝试过,现在我得到了输出。所以数据就在那里。 $this->set('_serialize', 'data');不一样吗?
猜你喜欢
  • 1970-01-01
  • 2015-01-29
  • 2018-08-23
  • 2023-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-12
  • 1970-01-01
相关资源
最近更新 更多