Post JSON to Codeigniter controller

后端 未结 12 1017
鱼传尺愫
鱼传尺愫 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:10

    make sure you have POST data, using $this->input->post() it will always return empty, you should put on the input type name $this->input->post('name_of_input_text')

    0 讨论(0)
  • 2020-12-30 05:11
    $post = json_decode($this->security->xss_clean($this->input->raw_input_stream));
    

    When you use $this->input->raw_input_stream you can read it multiple times and its basically the same as file_get_contents('php://input'). This works on CI3. I don't know if it works on CI2.

    0 讨论(0)
  • 2020-12-30 05:13

    Are you sure you're POSTing the data and not doing a GET instead? I ran into this issue earlier today (which is how I found this question) and I was doing a POST but using JSONP which seems to be done with a GET.

    CodeIgniter has a function called get_post that will get the data from wherever it happens to be.

    $this->input->get_post_string('data'); 
    

    I hope this helps you out.

    You can do it manually like so if you'd like.

    function get_post($index = '', $xss_clean = FALSE){
        if ( ! isset($_POST[$index]) )
        {
            return $this->get($index, $xss_clean);
        }
        else
        {
            return $this->post($index, $xss_clean);
        }
    }
    
    0 讨论(0)
  • 2020-12-30 05:14

    Try this code, it will output an array with all your parameters.

    $this->input->raw_input_stream;
    
    $input_data = json_decode($this->input->raw_input_stream, true);
    

    $input_data will return array

    0 讨论(0)
  • 2020-12-30 05:16

    Try this instead

    $json = $this->input->post('data');
    $json = stripslashes($json);
    $json = json_decode($json);
    print_r($json);
    

    You need to pass in the key of the data variable you want from the post array as an argument to post()

    0 讨论(0)
  • 2020-12-30 05:16

    I know this is an old post, but for others looking, this might be helpful:

    On the browser side, I create my data packet using code similar to this pattern:

        var form_data = { };
        $.each($('#mvt_dialog_form').serializeArray(), function() {
            form_data[this.name] = this.value;
        }); 
    
       // add the address data to the payload
       var result = { 
            form_data: form_data,
            locations: addressData,
            selected_location:  selectedLocation
        };
    
       // now wrap it all up with a pretty bow
       // Seriously, the key:value format is required for codeigniter INPUT class to be able to "see"
       var movement = {
           movement_dlg: JSON.stringify(result)
       };
    

    I then "post" movement to the server. In the controller, I then use the following logic:

        // Perform XSS filtering
        $postData = $this->input->post(NULL, TRUE);
        $result = json_decode($postData['movement_dlg']);
    
    0 讨论(0)
提交回复
热议问题