【问题标题】:posting data in $http post request在 $http 发布请求中发布数据
【发布时间】:2020-03-28 05:46:58
【问题描述】:

我正在尝试将表单中的数据发布到另一个域上的 API。请求正文返回为空。 当我去 devtools 时,有效载荷显示为 [object Object]

我尝试设置不同的标题,例如 'Content-Type' : 'application/json' 或 undefined 但没有运气

代码

$scope.form

{
   "name":"Test",
   "email":"test@test.com",
   "companyName":"company",
   "companySize":"6 - 10 employees",
   "manageTimeOff":true
}

提交函数

$scope.submitForm = function () {
    $scope.success = false;
    $scope.loading = true;

    $http({
        url: url,
        method: 'POST',
        data: $scope.form,
        withCredentials: false,
        headers: { "Content-Type": "application/x-www-form-urlencoded" },
        transformRequest: angular.identity
    }).then(function (response) {
        console.log(response);
        $scope.loading = false;

        if (response.data.Error) {
            $scope.success = false;
            swal("Something's gone wrong ☹", response.data.Error, "error")
        } else {
            $scope.success = true;
            swal({
                title: "Form Submitted",
                text: "We look forward to speaking with you soon!",
                icon: "success"
            });

            $scope.resetForm();
        }
    });
}

【问题讨论】:

  • 请给我们看$scope.form的日志
  • 内容类型x-www-form-urlencoded 已过时。设计后端以使用 AngularJS 框架默认支持的内容类型 application/json
  • 对不起,我的问题应该更清楚,$scope.form 是 submitForm() 上面的对象

标签: angularjs http


【解决方案1】:

内容类型x-www-form-urlencoded 已过时。设计后端以使用 AngularJS 框架默认支持的内容类型 application/json

$http({
    url: url,
    method: 'POST',
    data: $scope.form,
    withCredentials: false,
    ̶h̶e̶a̶d̶e̶r̶s̶:̶ ̶{̶ ̶"̶C̶o̶n̶t̶e̶n̶t̶-̶T̶y̶p̶e̶"̶:̶ ̶"̶a̶p̶p̶l̶i̶c̶a̶t̶i̶o̶n̶/̶x̶-̶w̶w̶w̶-̶f̶o̶r̶m̶-̶u̶r̶l̶e̶n̶c̶o̶d̶e̶d̶"̶ ̶}̶,̶
    ̶t̶r̶a̶n̶s̶f̶o̶r̶m̶R̶e̶q̶u̶e̶s̶t̶:̶ ̶a̶n̶g̶u̶l̶a̶r̶.̶i̶d̶e̶n̶t̶i̶t̶y̶
}).then(function (response) {
    //...
});

【讨论】:

  • 这有帮助,谢谢。我现在遇到了 cors 错误,但我想这是向前迈出的一步 ¯_(ツ)_/¯
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-09-23
  • 1970-01-01
  • 2020-09-26
  • 2012-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多