duagfgfn1981 2015-12-09 15:39
浏览 43
已采纳

Angular - 将HTTP发布数据发送到服务器[HOW TO]

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

  • 写回答

4条回答 默认 最新

  • doushi3202 2015-12-09 15:48
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?