HttpPost没有在angular js中向另一个页面发送数据


Http Post not sending data to another page in angular js

我正在Angular Js中处理登录表单。HTML和验证部分运行良好。

问题是当我使用HTTPPOST方法将数据发送到我的servicecall PHP页面并希望将其保存到DB时。

我已经尝试过在JS 中设置这个

headers: {'Content-Type': 'application/x-www-form-urlencoded'}

我已尝试将表单的enctype设置为

enctype="application/x-www-form-urlencoded" 

同样。但它们都不起作用,我无法通过$_REQUEST、$_POST、$_get中的任何一种方法获取数据。

我也尝试过使用PHP库函数。

$postdata = file_get_contents("php://input");

但它给出了一些奇怪的字符串,我无法处理,因为POST数据的数量可能是数百。

因此,还有其他方法可以解决这个问题。

http请求的代码为

var req = {
    method: 'POST',
    url: 'servicecall.php',
    headers: {'Content-Type': 'application/x-www-form-urlencoded'},
    data: { 
            username: $scope.loginData.username,
            password: $scope.loginData.password,
            action : 'checkLogin'
        }  //First way of sending data
//Second way of sending data which is also not working
//data: "{username:"+$scope.loginData.username+",password:"+$scope.loginData.password+",action:checkLogin}" 
}  
$http(req).then(function(response){
        console.log(response);  
   }, function(response){
        alert("Error "+response);
   });

在PHP页面上我做

print_r($_REQUEST);
print_r($_POST);

但它只打印空白数组,如array{}

下面是我用于相同目的的代码。希望这能对你有所帮助。

var app = angular.module('angularApp', []);
app.controller('loginCtrl', function ($scope, $http) {
    $scope.login = function () {
            var request = $http({
                method: "post",
                url: "login.php",
                data: {
                    email: $scope.email,
                    password: $scope.password
                },
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
            });
            /* Successful HTTP post request or not */
            request.success(function (data) {
                if(data == "1"){
                    $scope.responseMessage = "Successfully Logged In";
                }
                else {
                    $scope.responseMessage = "Username or Password is incorrect";
                }
            });
    }
});