【发布时间】: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() 上面的对象