Post JSON to Codeigniter controller

后端 未结 12 1087
鱼传尺愫
鱼传尺愫 2020-12-30 05:03

I am trying to receive and parse a JSON object sent in a POST request using Codeigniter but I cannot \"find\" it.

This is my controller code:

public          


        
12条回答
  •  感动是毒
    2020-12-30 05:31

    Firze's answer is correct but here is a more elaborated answer. I am not allowed to comment so I am posting it as an answer.

    It has to do with CodeIgniter not being able to fetch JSON. jQuery does some under the hood tricks and transforms your data into form-data-x, that's why it works when you don't specify the content type, don't encode your object, or other situations.

    If you want a pure JSON the solution is to use $this->input->raw_input_stream to fetch your JSON and decode it using php's json_decode. Check the full answer and code below:

    Retrieve JSON POST data in CodeIgniter

提交回复
热议问题