Javascript CakePHP 3:beforeRender()中的全局变量覆盖JSON视图中的其他变量
我使用的是CakePHP3.3.9。好吧,我的问题是:当我向操作发送ajaxget请求时,它不起作用。我用的是“前缀路由”。我已经找到了很多关于如何在CakePHP3中处理Ajax的帖子,但是在我的例子中什么都不起作用 这是我的代码:Javascript CakePHP 3:beforeRender()中的全局变量覆盖JSON视图中的其他变量,javascript,php,jquery,ajax,cakephp,Javascript,Php,Jquery,Ajax,Cakephp,我使用的是CakePHP3.3.9。好吧,我的问题是:当我向操作发送ajaxget请求时,它不起作用。我用的是“前缀路由”。我已经找到了很多关于如何在CakePHP3中处理Ajax的帖子,但是在我的例子中什么都不起作用 这是我的代码: // routes.php Router::extensions('json'); ... Router::prefix('myagendas', function ($routes) { $routes->connect('/agendas', ['c
// routes.php
Router::extensions('json');
...
Router::prefix('myagendas', function ($routes) {
$routes->connect('/agendas', ['controller' => 'agendas', 'action' => 'test']);
$routes->fallbacks(DashedRoute::class);
});
我在JS控制台中遇到此错误:
Fatal error</b>: Call to a member function config() on a non-object in AppController.php
我需要什么:现在我需要找到一种从视图访问.json扩展名的方法,并从视图中获取全局变量+其他变量。全局变量不应覆盖其他变量
谢谢 您可能在这里做错了什么:
class AgendasAppController extends AppController {
// there's no code here yet.. I'm just extending the AppController that has all the configs..
}
应该是这样的:
class AgendasController extends AppController {
}
问题是AppController中的变量“loggedIn”正在使用“\u serialize”并覆盖其他变量,因为它在beforeRender()方法中是全局变量。我只需要设置一个不带“\u serialize”的普通变量,现在它工作正常。每当收到错误时,请始终发布完整的错误,也就是说,包括完整的stacktrace(理想情况下是以正确可读的方式从日志中复制),即使这个问题对于熟悉CakePHP的人来说是显而易见的@ndm这是日志控制台中出现的唯一错误。它出现了两次,就是这样。我真的不知道发生了什么。正在记录CakePHP中的所有错误,包括stacktrace(假定启用了
Error.trace
选项,这是CakePHP应用程序模板中的默认选项),而且所有PHP错误都显示完整的文件路径和行号。@ndm问题已更新!您有什么想法吗?将您的url更改为“./myagents/agents/test.json”,并检查控制器中的($this->request->is(“get”))而不是ajax;仍然不工作,因为AppController中的全局变量beforeRender()方法正在覆盖对页面中其他变量的访问。
// scripts.js
$('#btnAdd').click(function () {
$.ajax({
type: 'GET',
url: './myagendas/agendas/test', //myagenda is a prefix
dataType: 'json',
success: function (data) {
console.log(data);
},
error: function (jqXHR, textStatus, errorThrown) {
console.log(jqXHR);
}
});
});
Fatal error</b>: Call to a member function config() on a non-object in AppController.php
// AppController.php
if ($this->request->session()->read('Auth.User.role') == 'admin') {
$this->set('loggedIn', true);
$this->set('_serialize', ['loggedIn']);
} else {
$this->set('loggedIn', false);
$this->set('_serialize', ['loggedIn']);
}
class AgendasAppController extends AppController {
// there's no code here yet.. I'm just extending the AppController that has all the configs..
}
class AgendasController extends AppController {
}