我在使用AngularJs
特定的“ PUT” Ajax请求(RESTFul服务)时遇到麻烦。
同一段代码可与jQuery
普通JavaScript一起使用。
$http({
method: "PUT",
url: "https://XXXXXXXXXXXXXX/api.php?rquest=updateUuid",
headers: {
"Content-type": "text/plain",
Authorization: "Basic " + btoa(email + ":" + password)
},
data: {
Email: email,
Uuid: login_response.uuid
}
}).success(function(response) {
console.log(response);
});
jQuery.ajax({
method: "PUT",
url: "https://XXXXXXXXXXXXXX/api.php?rquest=updateUuid",
headers: {
"Content-type": "text/plain",
Authorization: "Basic " + btoa(email + ":" + password)
},
data: {
Email: email,
Uuid: login_response.uuid
}
}).done(function(response){
console.log(response)
});
服务器正在响应一个JSON
对象,但是第一个代码(AngularJs
)的工作方式似乎有所不同。在我未设置Content-Type标头之前,我遇到了CORS问题(使用jQuery
,但是AngularJs
将content-type设置为“ application / json”并导致错误)。
我已经将第一部分固定为将内容类型设置为text / plain,但是现在我得到了400 Bad Request Error
(仅适用于AngularJs
)。我也尝试使用拦截器删除transformRequest,但是没有用。
我使用AngularJs
错了吗?因为我认为unlinkejQuery
会在后台执行额外的“填充”,制作一些额外的标题或无法正确传递数据。
当我不将任何数据传递给RESTful服务时,通常会收到该错误。
我正在学习AngularJs
,所以我宁愿用它代替jQuery
。任何帮助将非常感激。
非常感谢
我知道了,jQuery会自动将数据对象序列化为application / x-www-form-urlencoded,而AngularJs并不是...
工作代码:
$http({
method: "PUT",
url: "https://XXXXXXXXXXXXXX/api.php?rquest=updateUuid",
headers: {
Accept: "*/*",
"Content-Type": "application/x-www-form-urlencoded",
Authorization: "Basic " + btoa(email + ":" + password)
},
data: jQuery.param({
Email: email,
Uuid: login_response.uuid
})
})
无论如何,这看起来并不好,但至少可以正常工作。有人知道让AngularJs以x-www-form-urlencoded格式发送序列化数据的更好方法吗?
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句