JOSOSN AJAX location message arrives blank - codeigniter

I found many similar questions, but 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 at the PHP CI controller, $ this-> input-> post ('data') comes up 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.

+6
source share
5 answers

First, I would like to thank all the answers. Actually it was a couple of errors, First : as @bipen said, the data should be sent as an object, not a string. and when I tried this, it did not work because I did not put a single quote around the data

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

Second : as @foxmulder said, contentType has been sealed and should have ContentType so the correct code:

 $.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, if someone had trouble extracting PHP, here's 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; 
+12
source

send your data as an object, not a string .. (not sure if you already did this if we don’t see your data value .. if not, 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 you comment on the data

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

then data:data fine

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

bt you need to change the code codes to

  $this->input->post('sum') // = 2.250 $this->input->post('info') 
+4
source

contentType must be capitalized (ContentType)

see this question

+3
source

I extended the CI_Input class to allow json.

Put this in application / core / MY_input.php and you can usually use $ this-> input-> post () .

 class MY_Input extends CI_Input { public function __construct() { parent::__construct(); } public function post($index = NULL, $xss_clean = NULL){ if($xss_clean){ $json = json_decode($this->security->xss_clean($this->raw_input_stream), true); } else { $json = json_decode($this->raw_input_stream, true); } if($json){ if($index){ return $json[$index] ?? NULL; } return $json; } else { return parent::post($index, $xss_clean); } } } 

If you are using PHP5.x. Replace

 return $json[$index] ?? NULL; 

with

 return isset($json[$index]) ? $json[$index] : NULL; 
0
source

I am not familiar with CodeIgniter, but I think you can try with the global variable $_POST :

 var_dump($_POST['data']); 

If var_dump shows the data, maybe $this->input... has a problem

-1
source

Source: https://habr.com/ru/post/959039/


All Articles