简体   繁体   English

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

[英]PHP doesn't receive data sent from $http.post

I have a function inside an AngularJS controller that let me send POST data to a PHP page (it should be for authentication) 我在AngularJS控制器中有一个函数,它允许我将POST数据发送到PHP页面(它应该用于身份验证)

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

This function is called from this HTML code 从此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>

Using Firebug inside Firefox I can see that auth page is called and POST data is sent correctly 在Firefox中使用Firebug我可以看到调用auth页面并正确发送POST数据

在此输入图像描述

Problem raises within my auth PHP code: I try to retrieve POST data, but array is empty. 我的auth PHP代码中出现问题:我尝试检索POST数据,但数组为空。
I've also tried to write content in a file and I can confirm array is empty (file contains []) 我也尝试在文件中写内容,我可以确认数组是空的(文件包含[])

$userdata = $_POST;
file_put_contents("login.txt",json_encode($userdata)."\n", FILE_APPEND);

What's wrong with my code? 我的代码出了什么问题? I've also tried changing from POST to GET (using $http.get ), but $_GET array is empty too... 我也试过从POST改为GET(使用$http.get ),但是$ _GET数组也是空的......

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

$_JSON = json_decode(file_get_contents("php://input"), true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM