Keith Power
Keith Power

Reputation: 14141

cakePHP submitting a form with ajax

I am submitting a form with ajax and I am trying to return a json response. The problem is I get back missing view. When I add autoResponder=false I get nothing at all. I am using cakephp 2.5

I have another action in the same controller which I can get a json response back. I use a standard ajax call. The difference with this action is I am using form data with multipart. In the other ajax call I was able to add the extension .json to the url to tell cakephp to use that route.

When I add .json to the forms action url I get a response back missing action.

<form action="/btstadmin/pages/ajax_edit.json/3" id="ajax_editJsonForm" enctype="multipart/form-data" method="post" accept-charset="utf-8">

The response back

The action <em>ajax_edit.json</em> is not defined in controller <em>PagesController

My action is

public function ajax_edit() {

    if ($this->request->is('ajax')) {
        if ($this->Page->save($this->request->data)) {
            $this->RequestHandler->respondAs('json');
            $this->set("status", "success");
            $this->set("message", "All pages re-ordered");
            $this->set("_serialize", array("status", "message"));

My other action works fine with:

$.ajax({
        url: "/btstadmin/pages/reorder.json",
        type: "post",
        dataType:"json",
        data: neworderSer,

my action which gives a json response

public function reorder() {
    if ($this->Page->saveMany($data)) {
        $this->set("status", "sucess");
        $this->set("message", "All pages re-ordered");
        $this->set("content", $data);
        $this->set("_serialize", array("status", "message", "content"));

UPDATE

I changed the form create

$formaction = '/pages/ajax_edit/'.$this->data['Page']['id'].'.json';
echo $this->Form->create('Page', array('type' => 'file', 'url' => $formaction));

Upvotes: 0

Views: 1698

Answers (1)

ADmad
ADmad

Reputation: 8100

Your form's action url should be /btstadmin/pages/ajax_edit/3.json instead of /btstadmin/pages/ajax_edit.json/3. The url extension should always be at the end of url.

Upvotes: 1

Related Questions