javascript Cake PHP 3.0 中的 Jquery ajax 调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30108185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 11:41:40  来源:igfitidea点击:

Jquery ajax call in Cake PHP 3.0

javascriptphpjqueryajaxcakephp

提问by L_air



Hi, I'm trying to make a ajax request to the view A from the controller B like this :

嗨,我正在尝试从控制器 B 向视图 A 发出 ajax 请求,如下所示:

In the view A :

在视图 A 中:

var tab = new Array();
    function updateResult(){
            $.ajax({
                type:"POST",
                url:"<?php echo Router::url(array('controller'=>'B','action'=>'viewresult'));?>",
                dataType: 'text',
                async:false,
                success: function(tab){
                    alert('success');
                },
                error: function (tab) {
                    alert('error');
                }
            });
    }

In the controller B:

在控制器 B 中:

public function viewresult()
{
echo 'SUCCESS';
}

The problem is that in the 'response' of ajax, I've 'SUCCESS' but also the entire view A, I don't understand why... I want only 'SUCCESS'...

问题是在ajax的'响应'中,我有'SUCCESS'还有整个视图A,我不明白为什么......我只想要'SUCCESS'......

Thanks in advance !

提前致谢 !

采纳答案by marian0

The easiest way to achieve it is adding die()at the end of your function so it prevents to load whole layout:

实现它的最简单方法是die()在函数的末尾添加,这样可以防止加载整个布局:

public function viewresult()
{
    echo 'SUCCESS';
    die;
}

OR

或者

public function viewresult()
{
    die('SUCCESS');
}


But more conventional way is using JSONView. Your action should look as follows:

但更传统的方法是使用JSONView。您的操作应如下所示:

public function viewresult()
{
    $this->set('text', 'SUCCESS');
    $this->set('_serialize', ['text']);
}

You also have to load RequestHandlercomponent in initialize()method in your controller:

您还必须在控制器的方法中加载RequestHandler组件initialize()

public function initialize()
{
    parent::initialize();

    $this->loadComponent('RequestHandler');
}

You need to set allowed extensions for all routes connected later in routes.php:

您需要为稍后连接的所有路由设置允许的扩展routes.php

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

Now you can access your action adding extension .jsonat the end of it's URL, so you need to modify ajax call url:

现在您可以.json在其 URL 的末尾访问您的操作添加扩展名,因此您需要修改 ajax 调用 url:

url:"<?php echo Router::url(array('controller'=>'Main','action'=>'viewresult', '_ext' => 'json'));?>"

That's all, but keep in mind that this solution force you to handle JSON array in response. In this example output will be looks as follows:

这就是全部,但请记住,此解决方案会强制您处理 JSON 数组作为响应。在此示例中,输出将如下所示:

{"text": "SUCCESS"}

回答by Abhishek

Detect if its ajax as per following code in cakephp way :

按照cakephp方式中的以下代码检测其ajax是否:

    if($this->request->is('Ajax')) //Ajax Detection
    {
        $this->autoRender = false; // Set Render False
        $this->response->body('Success');
        return $this->response;
    }

Check here for more detectors - http://book.cakephp.org/3.0/en/controllers/request-response.html#Cake\Network\Request::addDetector

在此处查看更多检测器 - http://book.cakephp.org/3.0/en/controllers/request-response.html#Cake\Network\Request::addDetector

You can also use $this->Url->build instead of including Router for creating links in view.

您还可以使用 $this->Url->build 而不是包含 Router 来在视图中创建链接。

echo $this->Url->build(['action'=>'index']);