0

I have a question about javascript and cakephp, I need to send data via Post and recive it in the other side (the controller) and make the normal process that I already have. But I don't know how can I catch the data. I'm working with Ajax

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) {
				if (!$id) {
					throw new NotFoundException(__('Invalid post'));
				}
				$post = $this->Post->findById($id);
				if (!$post) {
					throw new NotFoundException(__('Invalid post'));
				}
				if ($this->request->is(array('post', 'put'))) {
					$this->Post->id = $id;
					if ($this->Post->save($this->request->data)) {
						$this->Session->setFlash(__('Your post has been updated.'));
						return $this->redirect(array('action' => 'index'));
					}
					$this->Session->setFlash(__('Unable to update your post.'));
				}
				if (!$this->request->data) {
					$this->request->data = $post;
				}
			}

1
  • When calling via Ajax, the data doesn't get sent the normal way. I don't quite remember which variable you can expect to find the data in, so check $this->request and $this->params->query.
    – Kai
    Oct 17, 2014 at 19:36

1 Answer 1

0

In that case you should send your id in URL. So even GET method is enough, because you controller receive $id param from URL.

So all what you need to change are post arguments:

$.ajax({
    type: 'POST',
    url: '/Posts/edit/' + id
})
1
  • Thanks so much, that was the answer!!
    – lll
    Oct 17, 2014 at 19:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.