Publicación al controlador con jquery ajax en CakePHP
Quiero publicar datos en un controlador en CakePHP, pero publicar con JQuery siempre produce un error y no puedo entender por qué.
En mi opinión, tengo el siguiente método, que publica los datos en la página del controlador
function RenameNode(name, id)
{
$.ajax({
type: "POST",
url: '<?php echo Router::url(array('controller' => 'categories', 'action' => 'rename')); ?>',
data: {
id: id,
name: name
},
success: function(){
}
});
}
Mi método de control se ve así:
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.'));
}
}
}
Cuando hago una publicación con el método jquery, recibo el siguiente mensaje de error en mi registro:
2013-05-20 11:34:25 Error: [NotFoundException] No id
Request URL: /cakephp/categories/rename
Stack Trace:
#0 [internal function]: CategoriesController->rename()
Cuando comento, las solicitudes de verificación de obtención y publicación, el controlador funciona perfectamente cuando lo llamo con / categories / rename? Id = 1 & name = test. Por alguna razón, la forma ajax no funciona, pero no puedo entender por qué. ¿Algunas ideas?
Actualizar
Lo arreglé cambiando el siguiente código, ahora funciona perfectamente
if(!$id)
{
$id = @$this->request->query('id');
}
if(!$name)
{
$name = @$this->request->query('name');
}
a
if(!$id)
{
$id = @$this->request->data('id');
}
if(!$name)
{
$name = @$this->request->data('name');
}