title: Retrieve JSON POST data in CodeIgniter
body: <p>I have been trying to retrieve JSON data from my php file.Its giving me a hard time.This is my code</p>
  
  <p>Code in my VIEW:</p>
  
  <pre><code>var productDetails = {'id':ISBNNumber,'qty':finalqty,'price':finalprice,'name':bookTitle};
  
          var base_url = '&lt;?php echo site_url() ?&gt;';
          $.ajax({
              url: "&lt;?php echo base_url() ?&gt;index.php/user/Add_to_cart/addProductsToCart",
              type: 'POST',
              data:productDetails,
              dataType:'JSON',
          });
  </code></pre>
  
  <p>Trying to retrieve in my Controller:</p>
  
  <pre><code>echo $this-&gt;input-&gt;post("productDetails");
  </code></pre>
  
  <p>Outputs Nothing.</p>
  
  <p>Here are my headers:</p>
  
  <pre><code>Remote Address:[::1]:80
  Request URL:http://localhost/CI/index.php/user/Add_to_cart/addProductsToCart
  Request Method:POST
  Status Code:200 OK
  Request Headersview source
  Accept:application/json, text/javascript, */*; q=0.01
  Accept-Encoding:gzip, deflate
  Accept-Language:en-US,en;q=0.8,fr;q=0.6
  Connection:keep-alive
  Content-Length:52
  Content-Type:application/x-www-form-urlencoded; charset=UTF-8
  Cookie:ci_session=3E5SPro57IrJJkjs2feMNlmMrTqEXrTNN8UyEfleeothNnHwNxuCZDSx4a7cJZGjj7fyr2KLpj%2BPNJeGRSzSPVmcFHVEdhSk4D47ziOl4eZcTUAZlQrWa3EYIeQJVWxMpiGZS26MEfbSXNmfel9e8TcsJTreZHipvfisrJovbXEAW4Uv%2BwrJRep1KCi1MMaDCVJb9UEinRVcDtYe%2F86jhn7kOj4kraVmVzx%2FsOaO0rAxLyAUtez%2Feaa4zBwpN3Td153sAoIb3WxVHoEj2oKyH5prVHigbIhIBR6XZqjBkM6hjBuoD2OSZ2wgLbp9DEENMoqui4WYyHROBuS2DYiJajblcS0KiFga5k%2FQOODvE7p6n%2BozN5ciDliVjJ4PnJ5PD1GaPEmec5%2FbQSlOHYWZk%2F2Blzw3Nw0EtLL7wKDzzQY%3Df645c36bb3548eb8de915b73f8763d97a47783ce
  Host:localhost
  Origin:http://localhost
  Referer:http://localhost/CI/index.php/user/view_available_books/viewAvailableBooks/5
  User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36
  X-Requested-With:XMLHttpRequest
  **Form Dataview** sourceview URL encoded
  id:234
  qty:1
  price:0.00
  name:dasdadsd2q3e!@!@@
  </code></pre>
  
  <p>My Response which I can See in Developer tools:</p>
  
  <pre><code>    Array
  (
      [id] =&gt; 234
      [qty] =&gt; 1
      [price] =&gt; 0.00
      [name] =&gt; dasdadsd2q3e!@!@@
  )
  </code></pre>
  
  <p>But in browser, the output is nothing. I am trying to solve it for more than 4 hours now but in vain.</p>
  
  <pre><code>print_r($_POST); // outputs nothing
  echo $data = file_get_contents('php://input'); //outputs nothing
  echo $id    = $this-&gt;input-&gt;post('productDetails');// outputs nothing
  </code></pre>
  
  <p><strong>My View Code:</strong></p>
  
  <pre><code>&lt;script&gt;
      $('#addtoCart').on('click',function(event){
          event.preventDefault();
          $(this).attr('disabled',"disabled");
          finalprice = $.trim($('#price').val());
          finalqty = $.trim($('#quantity').val());
  
          var productDetails = JSON.stringify({'id':ISBNNumber,'qty':finalqty,'price':finalprice,'name':bookTitle});
  
          var base_url = '&lt;?php echo site_url() ?&gt;';
          // console.log($);
          $.ajax({
              url: "&lt;?php echo base_url() ?&gt;index.php/user/Add_to_cart/addProductsToCart",
              type: 'POST',
              contentType: "application/json; charset=utf-8",
              data:productDetails,
              dataType:'html',
          });
  
  
      });
  &lt;/script&gt;
  </code></pre>
  
  <p><strong>Controller Code:</strong></p>
  
  <pre><code>function addProductsToCart(){
          var_dump(json_decode(file_get_contents("php://input")));
          print_r($_POST);
          // $data = json_decode($_POST["productDetails"]);
          // var_dump($data);
          // echo $data = file_get_contents('php://input');
  // print_r(json_decode($data));
          // $id    = $this-&gt;input-&gt;post('id');
          // $qty   = $this
  
      }
  </code></pre>