Codeigniter + Angular Js: How to receive JSON data

后端 未结 5 1121
盖世英雄少女心
盖世英雄少女心 2020-12-16 20:24

This is the situation:

I have a simple app made in Angular JS that comunicate with the server through an API made in codeigniter.

There is a

相关标签:
5条回答
  • 2020-12-16 20:26

    thanks for the reply. solution is as follows

    $obj=json_decode(file_get_contents('php://input'));
    

    you can test it by

    print_r(json_decode(file_get_contents('php://input')));
    
    0 讨论(0)
  • 2020-12-16 20:43

    $_POST will be empty in CodeIgniter because it purposely empties it for security reasons. You need to use $this->input->post(); instead.

    CodeIgniter Input Class

    0 讨论(0)
  • 2020-12-16 20:47

    Although this is already answered I quite often have this quick little utility included in many of my applications that need to accept both application/x-www-form-urlencoded and application/json POSTs.

    if (strcasecmp($_SERVER['REQUEST_METHOD'], 'post') === 0 && stripos($_SERVER['CONTENT_TYPE'], 'application/json') !== FALSE) {
        // POST is actually in json format, do an internal translation
        $_POST += json_decode(file_get_contents('php://input'), true);
    }
    

    After that point you can now just use the $_POST superglobal as you normally would, all the JSON data will be decoded for you.

    0 讨论(0)
  • 2020-12-16 20:49

    the data sent with the request is a name-value pairs so you should write some thing like that:

    data : {"myformdata":JSON.stringify({email:$scope.user.email, password:$scope.user.password})}
    

    and in code igniter you can recive the data :

    $this->input->post("myformdata"); //should return what
                                      // that JSON.stringify returned
    
    0 讨论(0)
  • 2020-12-16 20:50

    Use:

    $postData = $this->input->post();
    

    It should give you an array with all the post data.

    I also advise you to turn on XSS filtering.

    Following is the documentation for the Codeigniter Input Class: http://ellislab.com/codeigniter/user-guide/libraries/input.html

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