AJAX post JSOSN data arrives empty - codeigniter
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 ContentTypeso 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;
send your data as object and not string.. (not sure you have done that already unless we see you data's value.. if not then try it)
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:data} //<------here }); request.done(function(response){ alert('success'); }); request.fail(function(jqXHR, textStatus, errorThrown){ alert('FAILED! ERROR: ' + errorThrown); });
updatedif as of comments you data is
{"sum":"2.250","info":[{"id":"6","name":"bla","price":"1.000"}]}
then data:data
is fine
var request = $.ajax({ url : url, type : 'POST', contentType : 'application/json', data : data });
bt you need to change your codeigniter codes to
$this->input->post('sum') // = 2.250 $this->input->post('info')