也許有人可以幫我找到我的錯誤。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