php Angularjs $http POST 请求空数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19213903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 19:01:18  来源:igfitidea点击:

Angularjs $http POST request empty array

javascriptphpangularjs

提问by hawkharris

The following $http requestexecutes successfully, yet the PHP script on the other end receives an empty $_POSTarray when it should receive 'test' and 'testval.' Any ideas?

以下代码$http request成功执行,但另一端的 PHP 脚本在$_POST应该接收 'test' 和 'testval' 时接收到一个空数组。有任何想法吗?

$http({
    url: 'backend.php',
    method: "POST",
    data: {'test': 'testval'},
    headers: {'Content-Type': 'application/x-www-form-urlencoded'}
    }).success(function (data, status, headers, config) {
    console.log(data);

    }).error(function (data, status, headers, config) {});

回答by Gabor Kako

If you wan to send just that simple data, try this:

如果您只想发送那么简单的数据,请尝试以下操作:

$http({
    url: 'backend.php',
    method: "POST",
    data: 'test=' + testval,
    headers: {'Content-Type': 'application/x-www-form-urlencoded'}
    }).success(function (data, status, headers, config) {
        console.log(data);

    }).error(function (data, status, headers, config) {});

And php part shoul be like this:

而 php 部分应该是这样的:

<?php
    $data = $_POST['test'];
    $echo $data;
?>

It is working for me.

它对我有用。

回答by Bruno

This is a common issue with AngularJS.

这是 AngularJS 的常见问题。

The first step is to change the default content-typeheader for POSTrequest:

第一步是更改POST请求的默认内容类型标头:

$http.defaults.headers.post["Content-Type"] = 
    "application/x-www-form-urlencoded; charset=UTF-8;";

Then, using an XHR request interceptor, it is necessary to serialize properly the payload object:

然后,使用XHR 请求拦截器,有必要正确序列化有效负载对象:

$httpProvider.interceptors.push(['$q', function($q) {
    return {
        request: function(config) {
            if (config.data && typeof config.data === 'object') {
                // Check https://gist.github.com/brunoscopelliti/7492579 
                // for a possible way to implement the serialize function.
                config.data = serialize(config.data);
            }
            return config || $q.when(config);
        }
    };
}]);

In this way, payload data will be again available in the $_POSTarray.

这样,有效载荷数据将再次在$_POST数组中可用。

For more info about XHR interceptor.

有关XHR 拦截器的更多信息。

Another possibility, it is to mantain the default content-typeheader, and then server side parse the payload:

另一种可能性是保留默认的content-type标头,然后服务器端解析有效负载:

if(stripos($_SERVER["CONTENT_TYPE"], "application/json") === 0) {
    $_POST = json_decode(file_get_contents("php://input"), true);
}

回答by Rodolfo Jorge Nemer Nogueira

More simplified way:

更简单的方法:

myApp.config(function($httpProvider) {
    $httpProvider.defaults.transformRequest = function(data) {        
        if (data === undefined) { return data; } 
        return $.param(data);
    };
    $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8'; 
});

回答by KGolding

Remove the following line, and the preceding comma:

删除以下行和前面的逗号:

headers: {'Content-Type': 'application/x-www-form-urlencoded'}

And then the data will appear in $_POST. You only need that line if you are uploading a file, in which case you'll have to decode the body to get the data vars.

然后数据将出现在 $_POST 中。如果您上传文件,则只需要该行,在这种情况下,您必须解码正文以获取数据变量。

回答by notMyName

I found my solution here http://www.peterbe.com/plog/what-stumped-me-about-angularjs. There is a piece of code in the "AJAX doesn't work like jQuery" section, which solved my problem.

我在这里找到了我的解决方案http://www.peterbe.com/plog/what-stumped-me-about-angularjs。“AJAX 不像 jQuery 那样工作”部分有一段代码,解决了我的问题。