I am trying to send an ajax request to a Cakephp 3 controller.
我正在尝试向Cakephp 3控制器发送ajax请求。
The controller will find the data from the model and return it to the same view to be displayed,
控制器将从模型中找到数据并将其返回到要显示的同一视图中,
The view and the js function are as follows
视图和js功能如下
<script type = "text/javascript" language = "javascript">
$(document).ready(function(){
$("button").click(function(){
$.ajax({
type : "POST",
url : "<?php echo $this->Url->build(['controller' => 'users', 'action' => 'getByCategories', '_ext' => 'json']); ?>",
success: function (data){
console.log(JSON.stringify(data));
}
});
});
});</script>
The above function is in the index view of the controller.
上述功能位于控制器的索引视图中。
Upon the click of the button a request will be sent to the getByCategories function in the same controller.
单击按钮后,请求将被发送到同一控制器中的getByCategories函数。
The getByCategories function is as follows
getByCategories函数如下
public function getByCategories()
{
$id = 33;
$this->request->allowMethod(['ajax']);
$this->autoRender = false;
$cat = $this->Users->Categories->get($id);
if ($this->Users->Categories->childCount($cat) > 0)
{
$result = $this->Users->Categories->find('all', ['conditions' => ['parent_id' => $id]]);
$this->set(compact($result));
$this->set('_serialize', $result);
}
else
{
$result['Machines'] = $this->Users->Machines->find('all', ['conditions' => ['category_id' => $id]]);
$result['Medias'] = $this->Users->Medias->find('all', ['conditions' => ['category_id' => $id]]);
$this->set(compact($result));
$this->set('_serialize', $result);
}
}
The ajax response from this function is alway empty but the debugkit display the _serialize variable correctly.
此函数的ajax响应总是为空,但debugkit正确显示_serialize变量。
I have enabled the json extensions in the routes.php file. the variable for the this call in the debugkit is as follows
我在routes.php文件中启用了json扩展。 debugkit中此调用的变量如下所示
I would appreciated if someone can guide to get the variable from the debugkit in the ajax response
如果有人可以指导从ajax响应中的debugkit获取变量,我将不胜感激
Thank you in advance
先谢谢你
1 个解决方案
#1
1
Looks like you're close, except for a misunderstanding of how _serialize
(and maybe either set
or compact
) works. Check the manual for all the details, but what you should be setting in _serialize
is either a list of names of variables, or else true
to serialize everything.
看起来你很接近,除了误解_serialize(可能是set或compact)的工作方式。查看手册以获取所有详细信息,但是您应该在_serialize中设置的是变量名称列表,或者对序列化所有内容都是true。
Also, if you're using compact
with set
, you give it names of variables, not the variables themselves.
另外,如果你使用带有set的compact,你可以给它变量的名称,而不是变量本身。
So, what you should be using there is probably:
那么,你应该在那里使用的可能是:
$this->set(compact('result'));
$this->set('_serialize', ['result']);
#1
1
Looks like you're close, except for a misunderstanding of how _serialize
(and maybe either set
or compact
) works. Check the manual for all the details, but what you should be setting in _serialize
is either a list of names of variables, or else true
to serialize everything.
看起来你很接近,除了误解_serialize(可能是set或compact)的工作方式。查看手册以获取所有详细信息,但是您应该在_serialize中设置的是变量名称列表,或者对序列化所有内容都是true。
Also, if you're using compact
with set
, you give it names of variables, not the variables themselves.
另外,如果你使用带有set的compact,你可以给它变量的名称,而不是变量本身。
So, what you should be using there is probably:
那么,你应该在那里使用的可能是:
$this->set(compact('result'));
$this->set('_serialize', ['result']);