简体   繁体   English

如何用cakePHP执行ajax请求?

[英]how to perform an ajax request with cakePHP?

I know this should be obvious but I couldn't find anything useful or updated on the internet. 我知道这应该是显而易见的,但我在互联网上找不到任何有用或更新的东西。

I'm trying to perform a request with ajax that gets a view contents as well as a JS code to execute from cakePHP controller. 我正在尝试使用ajax执行请求,该请求获取视图内容以及从cakePHP控制器执行的JS代码。

The request is: 请求是:

  $.ajax({
    url: '/alarm/fetchGadgetsComponent',
    type: "POST",
    cache: false,
    dataType: 'json',
    success: function (data) {
       console.log(data);
    }
 });

and the PHP class looks like this: 并且PHP类看起来像这样:

class AlarmController extends AppController {
    public $uses = false;
    public $components = array('RequestHandler');
    public function index() {

    }


    public function fetchGadgetsComponent()
    {
        if ($this->request->is('Ajax'))
        {
            $this->autoRender = false;
            $this->layout = 'ajax';

            $html = $this->render('ajax\widgetsPanel');
            $js = $this->render('ajax\widgetsPanel.js');

            echo json_encode(array('html' => $html,'js' => $js));
        }
    }
}

First, the first render is just rendering it upon the screen instead into the $html variable. 首先,第一个渲染只是在屏幕上渲染它而不是$html变量。 Second, how can I get the js file with a different method? 其次,如何使用不同的方法获取js文件? (the render method is obviously not meant for this, as it searches for a .ctp file) and how to I parse them together as a json expression? (渲染方法显然不适用于此,因为它搜索.ctp文件)以及如何将它们作为json表达式一起解析?

Thank you 谢谢

I know that json_encode is php function that change array and other php variables to valid JSON, so you cannot get content of javascript file and change it to JSON using this method.The cake render method render the ctp files if you want change view prefix use: 我知道json_encode是php函数,它将数组和其他php变量更改为有效的JSON,因此你无法获取javascript文件的内容并使用此方法将其更改为JSON。蛋糕渲染方法渲染ctp文件,如果你想要更改视图前缀使用:

class AlarmController extends AppController {
    public $uses = false;
    public $components = array('RequestHandler');
    public function index() {

    }


    public function fetchGadgetsComponent()
    {
        if ($this->request->is('Ajax'))
        {
            $this->autoRender = false;
            $this->layout = 'ajax';

            $html = $this->render('ajax\widgetsPanel');

            $this->ext = '.js';
            $js = $this->render('ajax\widgetsPanel.js');

            echo json_encode(array('html' => $html,'js' => $js));
        }
    }
}

link to source code I think that better method is file_get_content function for js files. 链接到源代码我认为更好的方法是js文件的file_get_content函数。 Second for perform ajax request better is jsonView 第二个执行ajax请求更好的是jsonView

If this .js code is used by widgetsPanel, then this should be referneced from within the ctp file, most probably through js or html helpers of cakePHP, for this set the name of js file as a view variable and use it in ctp file. 如果这个.js代码被widgetsPanel使用,那么这应该从ctp文件中引用,最有可能是通过cakePHP的js或html帮助程序引用,为此设置js文件的名称作为视图变量并在ctp文件中使用它。

then you will not need to call $this->render() two times and a simple echo would be fine to display contents of widgetsPanel, so you will be savin one call to render method and call to json_encode. 然后你不需要调用$ this-> render()两次,一个简单的echo就可以显示widgetsPanel的内容了,所以你可以调用render方法并调用json_encode。 also change dataType to HTML, dataType JSON is for fetching only lean data not entire html view chunks. 也将dataType更改为HTML,dataType JSON仅用于获取精益数据,而不是整个html视图块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM