PHP不接收从$ http.post发送的数据

Mar*_*rco 2 php angularjs

我在AngularJS控制器中有一个函数,它允许我将POST数据发送到PHP页面(它应该用于身份验证)

$scope.submit = function () {
    $http.post(api + 'login.php', $scope.user)
};
Run Code Online (Sandbox Code Playgroud)

从此HTML代码调用此函数

<div>
    <span ng-show="isAuthenticated">{{welcome}}</span>
    <form ng-show="!isAuthenticated" ng-submit="submit()">
        <input ng-model="user.username" type="text" name="user" placeholder="Username" />
        <input ng-model="user.password" type="password" name="pass" placeholder="Password" />
        <input type="submit" value="Login" />
    </form>
    <div>{{error}}</div>
    <div ng-show="isAuthenticated">
        <a ng-click="logout()" href="">Logout</a>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

在Firefox中使用Firebug我可以看到调用auth页面并正确发送POST数据

在此输入图像描述

我的auth PHP代码中出现问题:我尝试检索POST数据,但数组为空.
我也尝试在文件中写内容,我可以确认数组是空的(文件包含[])

$userdata = $_POST;
file_put_contents("login.txt",json_encode($userdata)."\n", FILE_APPEND);
Run Code Online (Sandbox Code Playgroud)

我的代码出了什么问题?我也试过从POST改为GET(使用$http.get),但是$ _GET数组也是空的......

dav*_*ave 8

Angular将数据作为json发送,而不是作为表单数据,以检索它,您可以执行以下操作:

$_JSON = json_decode(file_get_contents("php://input"), true);
Run Code Online (Sandbox Code Playgroud)

  • 从来没有在我的生活中,我可以想象这......我已经失去了几个小时.非常感谢! (2认同)