也许有人可以帮我找到我的错误。jquery调用Cakephp 3控制器
在Cakephp中,我想使用jquery自动完成,当用户选择一个值时,检查控制器是否应该在数据库中存储新值。
这里是在控制器的视图
<?php
echo $this->Form->input('Name', array(
'label' => 'Anbieter',
'empty' => true,
'required' => 'required',
'templates' => array(
'inputContainer' => '{{content}}',
),
'class' => 'select anbieter',
'id' => 'Anbieter'
));
和脚本
<script>
jQuery('#Anbieter').autocomplete({
source:'<?php echo Cake\Routing\Router::url(array('controller' => 'Anbieter', 'action' => 'getAll')); ?>',
minLength: 0,
autoFill: false,
select: function(event, ui) {
$.ajax({
url: '<?php echo Cake\Routing\Router::url(array('controller' => 'Anbieter', 'action' => 'addCreate')); ?>',
success: function(event, ui){
console.log(ui);
},
error: function(event, ui){
console.log(ui);
}
});
}
});
的代码,我有这个
public function add()
{
$this->viewBuilder()->layout('other');
$anbieter = $this->Anbieter->newEntity();
if ($this->request->is('post')) {
$anbieter = $this->Anbieter->patchEntity($anbieter, $this->request->data);
if ($this->Anbieter->save($anbieter)) {
$this->Flash->success(__('The anbieter has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The anbieter could not be saved. Please, try again.'));
}
}
$this->set(compact('anbieter'));
$this->set('_serialize', ['anbieter']);
}
public function addCreate()
{
if($this->request->is('ajax')){
// do nothing for now
}
}
这只是虚拟的代码,所以请不要挑剔的阿博UT斯达康它;)
我的问题是,如果我用上面的代码,我会得到一个内部服务器错误,但如果我更改
url: '<?php echo Cake\Routing\Router::url(array('controller' => 'Anbieter', 'action' => 'addCreate')); ?>'
到
url: '<?php echo Cake\Routing\Router::url(array('controller' => 'Anbieter', 'action' => 'add')); ?>',
一切工作正常。 ..但是这并不是我想要的,所以我错过了什么?
非常感谢先进的
l.b.
编辑: 错误 toolbar.js:90 GET http://localhost/anbieter/add-create 500(内部服务器错误)AJAX的
无论何时收到错误,请始终发布** _complete_错误**,即**包括_full_ stacktrace **(理想情况下,从日志中以可正常读取的方式复制),即使问题对熟悉CakePHP的人来说可能很明显!也显示/描述正确的上下文,即显示/突出显示实际触发错误的代码,并且请始终提及您的_exact_ CakePHP版本 - 谢谢! – ndm