title: Posting to controller with jquery ajax in CakePHP
body: <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>