gpt4 book ai didi

jquery - POST 使用 JQuery 工作,但不能使用 Angularjs

转载 作者:行者123 更新时间:2023-12-01 07:49:12 25 4
gpt4 key购买 nike

任何人都可以帮我找出为什么这段代码工作正常:

       $.ajax({
type: "POST",
url: scope.serviceBase + "/api/Property/PostAsync",
contentType: "application/x-www-form-urlencoded",
processData: false,
data: data,
success: function (response) {
alert(response);
},
error: function (response) {
alert(response);
}
});

虽然仅使用 Angularjs 会失败:

      $http({
method: "POST",
url: scope.serviceBase + 'api/Property/PostAsync',
headers: {
'processData':false,
'Content-Type': "application/x-www-form-urlencoded"
},
data: data
}).success(function (response) {
alert(response);
}).error(function (response) {
alert(response);
});

仅供引用,我使用 AngularJs 格式得到的错误是 400 错误请求。

最佳答案

首先,processData 不是 header ,而且它在 Angular 中并不像在 jQuery 中那样存在。不过,它有一个 $httptransformRequest 属性,该属性接受一组函数,这些函数将在发送请求之前在 data 属性上执行。为了告诉 Angular 不要触摸您的数据,您只需将该配置设置为空数组即可。所以在你的情况下应该是这样的

$http({
method: "POST",
url: scope.serviceBase + 'api/Property/PostAsync',
headers: {
'Content-Type': "application/x-www-form-urlencoded"
},
data: data,
transformRequest: []
}).success(function (response) {
alert(response);
}).error(function (response) {
alert(response);
});

关于jquery - POST 使用 JQuery 工作,但不能使用 Angularjs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32031650/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com