Angularjs $ http POST请求空数组

时间:2022-08-22 17:38:04

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

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

$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) {});

5 个解决方案

#1


18  

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.

它对我有用。

#2


7  

This is a common issue with AngularJS.

这是AngularJS的常见问题。

The first step is to change the default content-type header for POST request:

第一步是更改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 $_POST array.

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

For more info about XHR interceptor.

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

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

另一种可能性是,它保留默认的内容类型头,然后服务器端解析有效负载:

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

#3


7  

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'; 
});

#4


3  

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中。如果要上传文件,则只需要该行,在这种情况下,您必须解码主体以获取数据变量。

#5


2  

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一样工作”部分中有一段代码,它解决了我的问题。

#1


18  

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.

它对我有用。

#2


7  

This is a common issue with AngularJS.

这是AngularJS的常见问题。

The first step is to change the default content-type header for POST request:

第一步是更改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 $_POST array.

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

For more info about XHR interceptor.

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

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

另一种可能性是,它保留默认的内容类型头,然后服务器端解析有效负载:

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

#3


7  

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'; 
});

#4


3  

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中。如果要上传文件,则只需要该行,在这种情况下,您必须解码主体以获取数据变量。

#5


2  

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一样工作”部分中有一段代码,它解决了我的问题。