Retrieve JSON POST data in CodeIgniter

后端 未结 5 966
独厮守ぢ
独厮守ぢ 2020-11-28 13:03

I have been trying to retrieve JSON data from my php file.Its giving me a hard time.This is my code

Code in my VIEW:

var productDetails = {\'id\':ISB         


        
相关标签:
5条回答
  • 2020-11-28 13:21

    Although OP seems satisfied, choosen answer doesn't tell us the reason and the real solution . (btw that post_array is not an array it's an object indeed ) @jimasun's answer has the right approach. I will just make things clear and add a solution beyond CI.

    So the reason of problem is ;

    Not CI or PHP, but your server doesn't know how to handle a request which has an application/json content-type. So you will have no $_POST data. Php has nothing to do with this. Read more at : Reading JSON POST using PHP

    And the solution is ; Either don't send request as application/json or process request body to get post data.

    For CI @jimasun's answer is precise way of that.

    And you can also get request body using pure PHP like this.

    $json_request_body = file_get_contents('php://input');
    
    0 讨论(0)
  • 2020-11-28 13:25

    General method I use for my Ajax Calls in CI :

    JS :

    post_array =
    {
        "myvar" : "value1",
        "myvar2": "value2"
    } 
    
    $.post(baseUrl + "/AjaxController/my_function", post_array,
        function(data)
        {
            var res = jQuery.parseJSON(data);
            alert(res.property);
        }  
    

    Controller :

    public function my_function()
    {
        $myvar = $this->input->post('myvar');
        $myvar2 = $this->input->post('myvar2'); 
    
        //Stuff
    
        echo json_encode($myobject);
    }
    
    0 讨论(0)
  • 2020-11-28 13:29

    You only have your own answer.

    print_r($_POST);

    Return :

    Array
    (
        [id] => 234
        [qty] => 1
        [price] => 0.00
        [name] => dasdadsd2q3e!@!@@
    )
    

    Then how will you get : echo $id = $this->input->post('productDetails');

    You will get id by echo $id = $this->input->post('id');

    0 讨论(0)
  • 2020-11-28 13:32

    I had the exact same problem. CodeIgniter doesn't know how to fetch JSON. I first thought is about the encoding Because I use fetch.js and not jQuery. Whatever I was doing I was getting an notting. $_POST was empty as well as $this->input->post(). Here is how I've solved the problem.

    Send request (as object prop -- because your js lib might vary):

    method: 'POST',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      ready: 'ready'
    })
    

    Node: I encode my data of type object into json. jQuery does this by itself when you set the dataType: 'JSON' option.

    CodeIgniter (3.1 in my case):

    $stream_clean = $this->security->xss_clean($this->input->raw_input_stream);
    $request = json_decode($stream_clean);
    $ready = $request->ready;
    

    Note: You need to clean your $this->input->raw_input_stream. You are not using $this->input->post() which means this is not done automatically by CodeIgniter.

    As for the response:

    $response = json_encode($request);
    header('Content-Type: application/json');
    echo $response;
    

    Alternatively you can do:

    echo $stream_clean;
    

    Note: It is not required to set the header('Content-Type: application/json') but I think it is a good practice to do so. The request already set the 'Accept': 'application/json' header.

    So, the trick here is to use $this->input->raw_input_stream and decode your data by yourself.

    0 讨论(0)
  • 2020-11-28 13:32

    I had the same problem but I found the solution.

    This is the Json that I am sending [{"name":"JOSE ANGEL", "lastname":"Ramirez"}]

    $data = json_decode(file_get_contents('php://input'), true);
    echo json_encode($data);
    

    This code was tested and the result is [{"name":"JOSE ANGEL","lastname":"Ramirez"}]

    0 讨论(0)
提交回复
热议问题