2014-10-17 20 views
0

我有一个关于javascript和cakephp的问题,我需要通过Post发送数据,并在另一端(控制器)中收回数据,并使我已有的正常进程。但我不知道如何捕捉数据。我与阿贾克斯在控制器中捕获数据

工作

function editFun(clicked_id){ 
 
    var id = clicked_id; 
 
    $("#Content").empty(); 
 
    $('#Content').html("<b>Loading response...</b>"); 
 
    $.ajax({ 
 
     type: 'POST', 
 
     url: '/Posts/edit', 
 
     data: (id) 
 
    }) 
 
    .done(function(data){ 
 
     console.log(data); 
 
     $('#Content').html(data); 
 
    }) 
 
    .fail(function(data){ 
 
     $('#Content').html(data); 
 
    }); 
 
}
public function edit($id = null) { 
 
\t \t \t \t if (!$id) { 
 
\t \t \t \t \t throw new NotFoundException(__('Invalid post')); 
 
\t \t \t \t } 
 
\t \t \t \t $post = $this->Post->findById($id); 
 
\t \t \t \t if (!$post) { 
 
\t \t \t \t \t throw new NotFoundException(__('Invalid post')); 
 
\t \t \t \t } 
 
\t \t \t \t if ($this->request->is(array('post', 'put'))) { 
 
\t \t \t \t \t $this->Post->id = $id; 
 
\t \t \t \t \t if ($this->Post->save($this->request->data)) { 
 
\t \t \t \t \t \t $this->Session->setFlash(__('Your post has been updated.')); 
 
\t \t \t \t \t \t return $this->redirect(array('action' => 'index')); 
 
\t \t \t \t \t } 
 
\t \t \t \t \t $this->Session->setFlash(__('Unable to update your post.')); 
 
\t \t \t \t } 
 
\t \t \t \t if (!$this->request->data) { 
 
\t \t \t \t \t $this->request->data = $post; 
 
\t \t \t \t } 
 
\t \t \t }

+0

当通过Ajax调用,数据不会发送的正常方式。我不记得哪个变量你可以期望找到数据,所以请检查'$ this-> request'和'$ this-> params-> query'。 – Kai 2014-10-17 19:36:30

回答

0

在这种情况下,你应该把你的ID的URL。所以即使GET方法也足够了,因为你的控制器从URL接收到了$id的参数。

因此,所有你需要改变什么岗位参数:

$.ajax({ 
    type: 'POST', 
    url: '/Posts/edit/' + id 
}) 
+0

非常感谢,这就是答案! – lll 2014-10-17 19:46:38

相关问题