【发布时间】:2013-03-07 07:01:53
【问题描述】:
我有一个带有标签ng-submit="login()的表单
该函数在 javascript 中可以正常调用。
function LoginForm($scope, $http)
{
$http.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
$scope.email = "fsdg@sdf.com";
$scope.password = "1234";
$scope.login = function()
{
data = {
'email' : $scope.email,
'password' : $scope.password
};
$http.post('resources/curl.php', data)
.success(function(data, status, headers, config)
{
console.log(status + ' - ' + data);
})
.error(function(data, status, headers, config)
{
console.log('error');
});
}
}
我从 PHP 文件收到 200 OK 响应,但是,返回的数据显示 email 和 password 未定义。这是我所有的php
<?php
$email = $_POST['email'];
$pass = $_POST['password'];
echo $email;
?>
知道为什么我得到未定义的POST 值吗?
编辑
我想指出,因为这似乎是一个流行的问题(但它很旧),.success 和 .error 已被弃用,您应该使用.then,正如@James Gentes 在评论中指出的那样
【问题讨论】:
-
您是否查看了开发人员工具的网络选项卡?
$http中传递了什么值? -
在网络选项卡中,
Form-Data下显示为{"email":"fsdg@sdf.com","password":"1234"} -
@Ronnie 看起来像 JSON。尝试
print_r($_POST);,然后在右侧索引上尝试json_decode() -
echo 'test';工作正常。我当然指向正确的文件 -
请注意 .success 和 .error 已被弃用并替换为 .then (docs.angularjs.org/api/ng/service/$http)
标签: php javascript angularjs