Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AJAX post JSOSN data arrives empty - codeigniter

I have found a lot of similar questions, yet no one is related to my question, however, 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') arrives empty!!

UPDATE This is my data: as shown before the AJAX request:

data = {"sum":"2.250","info":[{"id":"6","name":"bla","price":"1.000"}]}

Please help. Thanks in advance.

like image 422
Roshdy Avatar asked Nov 29 '13 11:11

Roshdy


2 Answers

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;
like image 96
Roshdy Avatar answered Nov 15 '22 20:11

Roshdy


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);
        });

updated if 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')
like image 32
bipen Avatar answered Nov 15 '22 19:11

bipen