P
P
paulvales2016-09-15 16:58:53
CodeIgniter
paulvales, 2016-09-15 16:58:53

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}]

send with ajax
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.
I checked, the data is gone. But they are not visible in the controller. Where did I go wrong?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ilya, 2016-09-15
@glebovgin

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.

M
Mikhail Osher, 2016-09-15
@miraage

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 question

Ask a Question

731 491 924 answers to any question