No params are send to PHP backend

Hi,

I’m a bit confused, I can’t understand why there are no params sending to the backend:

var data = {"email":'mail', 'password': 'pass'}; 
promise = $http({
                url: 'http://my_url',
                method: "POST",
                data: data
            }).then(function(response) {
                return response.data;
            });
return promise;

Within the backend I’m just saying:

print_r($_POST);

The request is working and I’m reaching the php without a problem, only my params are not there. I have no clue what I’m doing wrong?! I always get an empty array within console log:

Array
(
)

Thanks for any help,

Chris

Try in your run block of your app (app.js in the default ionic app)

.run(['$http', function ($http) {
    $http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
}]);

and in your php

header('Content-Type: application/json');
$request = json_decode(file_get_contents("php://input"));
$somedata = $request->somedata;
echo json_encode($somedata);

Then use the post request that you posted.

1 Like

Hi Claw,

thanks for your fast response. This simple line did the job for me:

$request = json_decode(file_get_contents("php://input"));

But I’m still not sure why the $_POST[] is not working…

Thanks again, you saved me a lot of time!