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
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')));
$_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
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.
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
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