Note that there are some explanatory texts on larger screens.

plurals
  1. POPosting to controller with jquery ajax in CakePHP
    primarykey
    data
    text
    <p>I want to post data to a controller in CakePHP, but posting with JQuery always results in an error and I can't figure out why.</p> <p>In my view I have the following method, that posts the data to the controller page</p> <pre><code>function RenameNode(name, id) { $.ajax({ type: "POST", url: '&lt;?php echo Router::url(array('controller' =&gt; 'categories', 'action' =&gt; 'rename')); ?&gt;', data: { id: id, name: name }, success: function(){ } }); } </code></pre> <p>My controller method looks like this:</p> <pre><code>public function rename($id = null, $name = null) { if ($this-&gt;request-&gt;is('get')) { throw new MethodNotAllowedException(); } if(!$id) { $id = @$this-&gt;request-&gt;query('id'); } if(!$name) { $name = @$this-&gt;request-&gt;query('name'); } if (!$id) { throw new NotFoundException(__('No id')); } $category = $this-&gt;Category-&gt;findById($id); if (!$category) { throw new NotFoundException(__('Invalid category')); } $this-&gt;autoRender = false; $this-&gt;layout = 'ajax'; if ($this-&gt;request-&gt;is('post') || $this-&gt;request-&gt;is('put')) { $this-&gt;Category-&gt;id = $id; $this-&gt;request-&gt;data['Category']['name'] = $name; if ($this-&gt;Category-&gt;save($this-&gt;request-&gt;data)) { $this-&gt;Session-&gt;setFlash(__('The category has been updated.')); $this-&gt;redirect(array('action' =&gt; 'index')); } else { $this-&gt;Session-&gt;setFlash(__('Unable to update the category.')); } } } </code></pre> <p>When I do a post with the jquery method, I keep getting the following error message in my log:</p> <pre><code>2013-05-20 11:34:25 Error: [NotFoundException] No id Request URL: /cakephp/categories/rename Stack Trace: #0 [internal function]: CategoriesController-&gt;rename() </code></pre> <p>When I comment the request checks for get and post, the controller itself works perfectly when I call it with /categories/rename?id=1&amp;name=test. For some reason the ajax way doesn't work, but I can't figure out why. Any ideas?</p> <p><strong>Update</strong></p> <p>I fixed it by changing the following code, now it works perfectly</p> <pre><code> if(!$id) { $id = @$this-&gt;request-&gt;query('id'); } if(!$name) { $name = @$this-&gt;request-&gt;query('name'); } </code></pre> <p>to</p> <pre><code> if(!$id) { $id = @$this-&gt;request-&gt;data('id'); } if(!$name) { $name = @$this-&gt;request-&gt;data('name'); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload