AngularJS HTTP post to PHP and undefined output
Angularjs .post()
defaults the Content-type header to application/json
. This will overide form-encoded data, however it will not change data
value to pass an appropriate query string, so PHP is not populating $_POST
as you expect.
Solution is use the default angularjs setting of application/json
as header, read the raw input in PHP, and then deserialize the JSON.
That can be achieved in PHP like this:
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$email = $request->email;
$pass = $request->password;
You can do it on the server side also for $_POST, at the beginning of my init file,
if ($_SERVER['REQUEST_METHOD'] == 'POST' && empty($_POST))
$_POST = json_decode(file_get_contents('php://input'), true);