大佬教程收集整理的这篇文章主要介绍了AngularJS HTTP发布到PHP并且未定义,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
angularJs .post()
默认将Content-
type标头设置为application/Json
。您要覆盖此内容以传递表单编码的数据,但是您没有更改data
值以传递适当的查询字符串,因此php并未$_POST
按预期填充。
我的建议是只使用默认的angularJs设置application/Json
作为标头,读取php中的原始输入,然后反序列化JsON。
可以这样在php中实现:
$postdata = file_get_contents("php://input");
$request = Json_decode($postdata);
$email = $request->email;
$pass = $request->password;
或者,如果您严重依赖$_POST
功能,则可以形成类似的查询字符串email=someemail@email.com&password=somepassword
并将其作为数据发送。确保此查询字符串是URL编码的。如果是手动构建的(而不是使用类似的东西jquery.serialize()
),则JavaScriptencodeURIComponent()
可以为您解决问题。
我有一个带有标签的表格 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)
.@R_696_6048@s(function(data,status,headers,config)
{
console.log(status + ' - ' + data);
})
.error(function(data,config)
{
console.log('error');
});
}
}
我从php文件中获得了200 OK响应,但是返回的数据就是这样,email
并且password
未定义。这就是我拥有的所有php
<?php
$email = $_POST['email'];
$pass = $_POST['password'];
echo $email;
?>
知道为什么我会得到未定义的POST
值吗?
编辑
我想指出,因为这似乎是一个受欢迎的问题(但它是旧的),.@R_696_6048@s
并.error
已被弃用,你应该使用.then
如@詹姆斯氏族在commments指出
以上是大佬教程为你收集整理的AngularJS HTTP发布到PHP并且未定义全部内容,希望文章能够帮你解决AngularJS HTTP发布到PHP并且未定义所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。