1

I'm trying to send data from my login FORM to backend writen in PHP using POST method.

my Angular code looks like:

$scope.getToken = function(){
    // console.log $scope.login to make sure I'm not sending empty data
    console.log($scope.login);
    $http({
        method: 'POST',
        url: '../../api/v1/Oauth.php',
        data: { 'login' : $scope.login, 'password' : $scope.password }
    }).then(function successCallback(response) {
        console.log(response);
    }, function errorCallback(response) {
       console.log(response);
    });
};

and after that I try to catch it on my PHP:

if((isset($_POST['login']) AND isset($_POST['password'])))
{
    $username = $_POST['login'];
    $password = $_POST['password'];
    echo $username;
}
else
    var_dump($_POST);

This statement always go to else and return empty array. Can someone advise me what I'm doing wrong or how can I debug this? Because it looks that I send data fron angular correctly but it didn't come to server.

Thanks Kind Regards Andurit

Andurit
  • 5,612
  • 14
  • 69
  • 121
  • 1
    can you open up the developer tools and examine the request body. Does it look like what you expect? – pQuestions123 Dec 09 '15 at 15:43
  • 1
    Did you mean `'username' : $scope.username` instead of `'login' : $scope.login ?` – Komo Dec 09 '15 at 15:43
  • hey @Komo good point, anyway this should atleast dump data in else statemenet which is not doing. But thanks I will edit this (+ THUMB UP for you ) – Andurit Dec 09 '15 at 15:44
  • @pQuestions123 reuqest looks like: { login: "Andurit" password: "ads" } which looks fine for me. – Andurit Dec 09 '15 at 15:50

4 Answers4

3

Use this:

json_decode(file_get_contents('php://input'));

Check your network tab in your developer bar. You can see that you send payload data in the http body. That's why the $_POST array is empty.

schellingerht
  • 5,726
  • 2
  • 28
  • 56
2

Some older server side web libraries like Coldfusion/.NET/PHP have issues grabbing a POST BODY by default (which is how $http sends the data).

You can reference How to get body of a POST in php? to learn how to write your PHP in a way that it will accept the current and correct standard of sending data via a post.

To access the entity body of a POST or PUT request (or any other HTTP method):

$entityBody = file_get_contents('php://input');

Also, the STDIN constant is an already-open stream to php://input, so you can alternatively do:

$entityBody = stream_get_contents(STDIN);
Community
  • 1
  • 1
Sean Larkin
  • 6,290
  • 1
  • 28
  • 43
  • 1
    Hey @Sean Larking , I already accept Henris answer, howwever thumbup for you for this explenation :) – Andurit Dec 09 '15 at 15:59
0

try:

data: { login : $scope.login, password : $scope.password }
pQuestions123
  • 4,471
  • 6
  • 28
  • 59
0
$http.post('url', {login: 'Alex', password: 'qwerty'}).then(function(){},function(){});
Alex Repeckiy
  • 173
  • 10