AJAX post JSON data arrives empty

ajax, codeigniter, javascript, jquery, json

Solution

First I'd like to thank all responses. Actually it was a couple of mistakes, First: as @bipen said, data must be sent as an object rather than a string. and when I tried it, it didn't work because I didn't put the single-quote around data

$.ajax({
  url: url,
  type: 'POST',
  contentType: 'application/json',
  data: {'data': data}
});

Second: as @foxmulder said, contentType was misspelled, and should be ContentType so the correct code is:

$.ajax({
  url: url,
  type: 'POST',
  ContentType: 'application/json',
  data: {'data': data}
}).done(function(response){
  alert('success');
}).fail(function(jqXHR, textStatus, errorThrown){
  alert('FAILED! ERROR: ' + errorThrown);
});

and just FYI in case someone had issues with PHP fetching, this is how to do it:

$data = $this->input->post('data');
    $data = json_decode($data);
    $sum = $data->sum;
    $info_obj = $data->info;
    $item_qty = $info_obj[0]->quantity;

Problem

This is my AJAX request ``` data = JSON.stringify(data); url = base_url + "index.php/home/make_order"; //alert(url); var request = $.ajax({ url: url, type: 'POST', contentType: 'application/json', data: data }); request.done(function(response){ alert('success'); }); request.fail(function(jqXHR, textStatus, errorThrown){ alert('FAILED! ERROR: ' + errorThrown); }); ``` My problem is that when it arrives to the PHP CI-controller `$this->input->post('data')`, it is empty. This is my data: as shown before the AJAX request: ``` data = {"sum":"2.250","info":[{"id":"6","name":"bla","price":"1.000"}]} ```

Original source