title: cakephp 2.2 retrieve json data in controller
body: <p>I'm trying to send JSON data from a web page using JQuery, like this:</p>
  
  <pre><code>$.ajax({
      type: "post",       // Request method: post, get
      url: "http://localhost/ajax/login",
      data: '{username: "wiiNinja", password: "isAnub"}',
      dataType: "json",   // Expected response type
      contentType: "application/json",
      cache: false,
      success: function(response, status) {
          alert ("Success");
      },
      error: function(response, status) {
          alert('Error! response=' + response + " status=" + status);
      }
  });
  </code></pre>
  
  <p>In cake2.2, I have a controller named Ajax that has a method named "login", like this:</p>
  
  <pre><code>public function login($id = null)
  {
      if ($this-&gt;RequestHandler-&gt;isAjax())
      {
          $this-&gt;layout = 'ajax'; // Or $this-&gt;RequestHandler-&gt;ajaxLayout, Only use for HTML
          $this-&gt;autoLayout = false;
          $this-&gt;autoRender = false;
  
          $response = array('success' =&gt; false);
  
          $data = $this-&gt;request-&gt;input(); // MY QUESTION IS WITH THIS LINE
          debug($data, $showHTML = false, $showFrom = true);
      }
      return;
  }
  </code></pre>
  
  <p>I just want to see if I'm passing in the correct data to the controller. If I use this line:</p>
  
  <pre><code>$data = $this-&gt;request-&gt;input();
  </code></pre>
  
  <p>I can see the debug printout: </p>
  
  <pre><code>{username: "wiiNinja", password: "isCool"}
  </code></pre>
  
  <p>I read in the CakePHP manual 2.x, under "Accessing XML or JSON data", it suggests this call to decode the data:</p>
  
  <pre><code>$data = $this-&gt;request-&gt;input('json_decode');
  </code></pre>
  
  <p>When I debug print $data, I get "null". What am I doing wrong? Is my data passed in from the Javascript incorrect? Or am I not calling the decode correctly?</p>
  
  <p>Thanks for any suggestion.</p>
  
  <p>============= My own Edit ========</p>
  
  <p>Found my own mistake through experiments:</p>
  
  <p>When posting through Javascript, instead of this line:</p>
  
  <pre><code>data: '{username: "wiiNinja", password: "isAnub"}',
  </code></pre>
  
  <p>Change it to:</p>
  
  <pre><code>data: '{"username": "wiiNinja", "password": "isAnub"}',
  </code></pre>
  
  <p>AND</p>
  
  <p>In the controller code, change this line:</p>
  
  <pre><code>$data = $this-&gt;request-&gt;input('json_decode');
  </code></pre>
  
  <p>To:</p>
  
  <pre><code>$data = $this-&gt;request-&gt;input('json_decode', 'true');
  </code></pre>
  
  <p>It works.</p>
  
  <hr>
  
  <p>Dunhamzzz,</p>
  
  <p>When I followed your suggestions, and examine the "$this->request->params" array in my controller code, it contains the following:</p>
  
  <pre><code>array(
      'plugin' =&gt; null,
      'controller' =&gt; 'ajax',
      'action' =&gt; 'login',
      'named' =&gt; array(),
      'pass' =&gt; array(),
      'isAjax' =&gt; true
  )
  </code></pre>
  
  <p>As you can see, the data that I'm looking for is not there. I've already got the the proper routes code. This is consistent with what the documentation for 2.x says here:</p>
  
  <p><a href="http://book.cakephp.org/2.0/en/controllers/request-response.html" rel="noreferrer">http://book.cakephp.org/2.0/en/controllers/request-response.html</a></p>
  
  <p>So far, the only way that I found to make it work, is as stated above in "My own Edit". But if sending a JSon string to the server is not the right thing to do, I would like to fix this, because eventually, I will have to handle third party code that will send JSon objects.</p>