【问题标题】:$http Post to php backend, but the server side gets nothing$http Post 到 php 后端,但服务器端什么也得不到
【发布时间】:2014-06-11 14:54:11
【问题描述】:

我正在学习 AngularJS,并尝试使用以下编码将转储数据发布到 php 后端。

angular.module('app.customerModule', [])
    .factory('customerFactory', function($scope, $http) {
        return {
            var customer = {customer: '1234'};
            httpNewCustomer: function(callback) {
                $http.post('http://domain.local/customer_new.php', )
                  .success(function(data) {
            })
        }
    }
})
.controller('customerController', function($rootScope, $scope, customerFactory) {
    $scope.newCustomer = function() {       
        customerFactory.httpNewCustomer(function(dataResponse) {
        });
    }
});

不幸的是,在服务器端,$_POST 一无所获;

这就是 http 标头的样子。

我也尝试过这种替代编码

httpNewCustomers: function(callback) {
    var postData = {customer: '2345'};

    $http({
        method: 'POST',
        url: 'http://domain.local/customer_new.php',
        data: postData,
        headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'}
    })
        .success(function(data) {
        })
}

这就是 http 标头的样子。

当我使用这种编码尝试使用 jQuery 时,一切都很好。

var postData = {customer: '3456'};

$.ajax({
    type: 'POST',
url: 'http://domain.local/customer_new.php',
dataType: 'json',
data: postData,
success: function(data) {
    // console.log(data);
}
});

请帮我配置 $http 以将数据发布到 php 后端。

【问题讨论】:

    标签: angularjs http-post


    【解决方案1】:

    angular 默认只支持 json 请求转换器。如您所见,您的两个角度请求都有数据,但它们是 json。您要么需要更改服务器以使其能够解析 json,要么添加请求转换器以使数据采用表单编码格式。

    您可以在此处阅读有关 $http 转换器的更多信息:https://docs.angularjs.org/api/ng/service/$http

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-15
      • 2019-04-06
      • 1970-01-01
      • 2018-10-19
      • 2015-10-24
      相关资源
      最近更新 更多