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

查看:24
本文介绍了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天全站免登陆