gpt4 book ai didi

javascript - 如何使用 Javascript 在 $.post() 方法中传递授权 header ?

转载 作者:数据小太阳 更新时间:2023-10-29 06:02:06 24 4
gpt4 key购买 nike

我想在将数据发布到服务器时传递授权 header 。我试过了

$.ajax({
url : <ServiceURL>,
data : JSON.stringify(JSonData),
type : 'POST',
contentType : "text/html",
dataType : 'json',
success : function(Result) {
},
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', <Authorization Header Value>);
},
error: function (RcvData, error) {
console.log(RcvData);
}
});

但是 REST 服务返回错误(错误代码:500)。在添加授权之前,相同的服务在 $.post() 上运行良好。谁能告诉我“如何在 $.post() 中传递授权 header ?”

最佳答案

使用

 contentType: 'application/json',

您可能混淆了 datacontentType

  • contentType 是您发送的 Content-type header 。

  • data 改变了 jQuery 处理您收到的数据的方式。

关于javascript - 如何使用 Javascript 在 $.post() 方法中传递授权 header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21276178/

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