AngularJS HTTP发布到PHP,并且未定义 [英] AngularJS HTTP post to PHP and undefined

查看:81
本文介绍了AngularJS HTTP发布到PHP,并且未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个标签为ng-submit="login()

该函数在javascript中被很好地调用.

The function gets called fine in 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响应,但是返回的数据表明emailpassword是未定义的.这就是我所有的PHP

I am getting a 200 OK response back from the PHP file, however, the returned data is saying that email and password are undefined. This is all the php I have

<?php
$email = $_POST['email'];
$pass  = $_POST['password'];
echo $email;
?>

知道为什么我会得到未定义的POST值吗?

Any idea why I am getting undefined POST values?

编辑

我想指出,因为这似乎是一个很受欢迎的问题(但它已经很老了),.success.error已过时,您应该使用.then,就像@James Gentes在评论中指出的那样

I wanted to point out since this seems to be a popular question (yet it is old), .success and .error have been deprecated and you should use .then as @James Gentes pointed out in the commments

推荐答案

angularjs .post()默认将Content-type标头设置为application/json.您要覆盖此内容以传递表单编码的数据,但是您没有更改data值以传递适当的查询字符串,因此PHP并未按您期望的那样填充$_POST.

angularjs .post() defaults the Content-type header to application/json. You are overriding this to pass form-encoded data, however you are not changing your data value to pass an appropriate query string, so PHP is not populating $_POST as you expect.

我的建议是仅使用application/json的默认angularjs设置作为标题,读取PHP中的原始输入,然后反序列化JSON.

My suggestion would be to just use the default angularjs setting of application/json as header, read the raw input in PHP, and then deserialize the JSON.

可以在PHP中这样实现:

That can be achieved in PHP like this:

$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()之类的东西),则Javascript的encodeURIComponent()应该可以帮您解决问题.

Alternately, if you are heavily relying on $_POST functionality, you can form a query string like email=someemail@email.com&password=somepassword and send that as data. Make sure that this query string is URL encoded. If manually built (as opposed to using something like jQuery.serialize()), Javascript's encodeURIComponent() should do the trick for you.

这篇关于AngularJS HTTP发布到PHP,并且未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆