在CakePHP中使用jQuery Ajax发布到控制器 [英] Posting to controller with jquery ajax in CakePHP
问题描述
我想将数据发布到CakePHP中的控制器,但是使用JQuery进行发布始终会导致错误,并且我不知道为什么。
I want to post data to a controller in CakePHP, but posting with JQuery always results in an error and I can't figure out why.
在我看来我有以下方法,将数据发布到控制器页面
In my view I have the following method, that posts the data to the controller page
function RenameNode(name, id)
{
$.ajax({
type: "POST",
url: '<?php echo Router::url(array('controller' => 'categories', 'action' => 'rename')); ?>',
data: {
id: id,
name: name
},
success: function(){
}
});
}
我的控制器方法如下:
public function rename($id = null, $name = null) {
if ($this->request->is('get')) {
throw new MethodNotAllowedException();
}
if(!$id)
{
$id = @$this->request->query('id');
}
if(!$name)
{
$name = @$this->request->query('name');
}
if (!$id) {
throw new NotFoundException(__('No id'));
}
$category = $this->Category->findById($id);
if (!$category) {
throw new NotFoundException(__('Invalid category'));
}
$this->autoRender = false;
$this->layout = 'ajax';
if ($this->request->is('post') || $this->request->is('put')) {
$this->Category->id = $id;
$this->request->data['Category']['name'] = $name;
if ($this->Category->save($this->request->data)) {
$this->Session->setFlash(__('The category has been updated.'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('Unable to update the category.'));
}
}
}
我在jQuery方法,我在日志中不断收到以下错误消息:
When I do a post with the jquery method, I keep getting the following error message in my log:
2013-05-20 11:34:25 Error: [NotFoundException] No id
Request URL: /cakephp/categories/rename
Stack Trace:
#0 [internal function]: CategoriesController->rename()
当我评论请求检查get和post时,当我使用/ categories / rename?id进行调用时,控制器本身可以完美地工作= 1& name =测试。由于某种原因,ajax方式不起作用,但我不知道为什么。有任何想法吗?
When I comment the request checks for get and post, the controller itself works perfectly when I call it with /categories/rename?id=1&name=test. For some reason the ajax way doesn't work, but I can't figure out why. Any ideas?
更新
我通过更改以下代码对其进行了修复,现在可以正常使用了
I fixed it by changing the following code, now it works perfectly
if(!$id)
{
$id = @$this->request->query('id');
}
if(!$name)
{
$name = @$this->request->query('name');
}
至
if(!$id)
{
$id = @$this->request->data('id');
}
if(!$name)
{
$name = @$this->request->data('name');
}
推荐答案
您不包括<您发布到的URL中的code> id 和/或 name
;
echo Router::url(array('controller' => 'categories', 'action' => 'rename'));
会输出;
/categories/rename
但是您正在期待
/categories/rename/1/test
或
/categories/rename?id=1&name=test
将AJAX代码中的URL更改为类似的内容;
Change the URL in your AJAX code to something like;
echo Router::url(array(
'controller' => 'categories',
'action' => 'rename',
0 => $this->request->params['pass'][0],
1 => $this->request->params['pass'][1]
));
应该输出正确的网址,其中包含原始 <$ c $当前请求的c> id 和 name
(例如/ categories / rename / 123 / oldname)
Which should output the right url, containing the original id
and name
of the current request (e.g. /categories/rename/123/oldname)
这篇关于在CakePHP中使用jQuery Ajax发布到控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!