【发布时间】:2015-11-04 05:14:22
【问题描述】:
我正在 Angular Js 中处理登录表单。HTML 和验证部分工作正常。
问题是当我使用 HTTP POST 方法将数据发送到我的服务调用 PHP 页面并希望将其保存到数据库时。
我已经尝试了通过在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{}这样的空白数组
【问题讨论】:
-
我们能看到完整的
$http({})对象吗?我怀疑在将数据发送到服务器之前如何格式化数据存在问题。 -
请提供$http调用的代码。好像有问题。
-
你能添加代码片段吗
-
@Ohgodwhy 我也添加了 http 代码。
-
您在控制台中得到了什么?