Answer the question
In order to leave comments, you need to log in
How to parse such json in CI?
Greetings! I work in codeigniter, the following json is formed:
[{"kind":24,"val":"2","day":1},{"kind":24,"val":"2","day":2},{"kind":24,"val":"2","day":3}]
var data_array = new Array();
$("input[type='checkbox']").each(function(){
var item = {};
for(var i in $(this).data()){
item[i] = $(this).data(i);
item['val'] = $(this).val();
}
data_array.push(item);
});
var data = JSON.stringify(data_array);
$.ajax({
type:"POST",
url:"/welcome/savejournal",
contentType : "application/json",
data: data,
success:function(data){ console.log(data); },
error:function(data){console.warn(data);}
})
, in the controller I accept var_dump($this->input->post()) shows an empty array. Answer the question
In order to leave comments, you need to log in
Good afternoon.
1. Try to accept just $_POST - if it is not empty, then the problem is inside the CI's Input class
2. Just in case, make sure that you do not add the last slash to the urls in htaccess, if you do, then $_POST will come empty, should be sent to url: "/welcome/savejournal / "
3. For the test, send not POST but GET and see if there is data or not.
4. Just make sure that the controller does not die with a 500 error. You never know.
Handles json_decode($this->input->raw_input_stream, true)
. Better yet, wrap it in middleware.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question