Post JSON to Codeigniter controller

后端 未结 12 1016
鱼传尺愫
鱼传尺愫 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: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']);
    

提交回复
热议问题