0

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));
Keith Power
  • 13,891
  • 22
  • 66
  • 135

1 Answers1

1

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.

ADmad
  • 8,102
  • 16
  • 18
  • thanks, I did think of this but I am unsure how to add it echo $this->Form->create('Page', array('type' => 'file', 'action' => 'ajax_edit')); – Keith Power Jun 12 '14 at 20:05
  • 1
    Using the 'url' key. `$this->Form->create('Page', array('type' => 'file', 'url' => array('action' => 'ajax_edit', 'ext' => 'json')));` – ADmad Jun 13 '14 at 04:50