相关疑难解决方法(0)

Cakephp3:如何返回json数据?

我正在调用cakePhp控制器的ajax调用:

$.ajax({
                type: "POST",
                url: 'locations/add',
                data: {
                  abbreviation: $(jqInputs[0]).val(),
                  description: $(jqInputs[1]).val()
                },
                success: function (response) {
                    if(response.status === "success") {
                        // do something with response.message or whatever other data on success
                        console.log('success');
                    } else if(response.status === "error") {
                        // do something with response.message or whatever other data on error
                        console.log('error');
                    }
                }
            });
Run Code Online (Sandbox Code Playgroud)

当我尝试这个时,我收到以下错误消息:

控制器操作只能返回Cake\Network\Response或null.

在AppController中我有这个

$this->loadComponent('RequestHandler');
Run Code Online (Sandbox Code Playgroud)

启用.

Controller函数如下所示:

public function add()
{
    $this->autoRender = false; // avoid to render view

    $location = $this->Locations->newEntity();
    if ($this->request->is('post')) {
        $location = …
Run Code Online (Sandbox Code Playgroud)

json cakephp

12
推荐指数
3
解决办法
2万
查看次数

标签 统计

cakephp ×1

json ×1