我正在尝试使用 fetch
api。
首先我创建了一个 new Headers()
对象:
var oHeaders = new Headers({
'Accept': 'application/json',
'Content-Type': 'application/json',
"X-DocuSign-Authentication": '{"Username":"xxx","Password":"xxx","IntegratorKey":"xxx"}'
})
在 header 实例化后,如果我尝试记录 header ,一切都是正确的。
oHeaders.forEach(function(v){console.log(v)})
//logs: 2 application/json {"Username":"xxx","Password":"xxx","IntegratorKey":"xxx"}
我创建请求对象:
var oReq = new Request('https://eu.docusign.net/restapi/v2/login_information', {
method: 'GET',
headers: oHeaders,
mode: 'no-cors',
});
如果我尝试记录请求对象的 header ,只有 accept
header 会在那里。
oReq.headers.forEach(function(v){console.log(v)})
//logs: application/json
如果我尝试 fetch(oReq)
,我会收到 401 unauthorized
响应。
是什么让标题消失了?
最佳答案
当您为请求设置 mode: 'no-cors'
时,浏览器将不允许您设置除 CORS-safelisted request-headers 之外的任何请求 header 。 .参见 the spec requirements :
To append a name/value (name/value) pair to a
Headers
object (headers), run these steps:
- Otherwise, if guard is "
request-no-cors
" and name/value is not a CORS-safelisted request-header, return.
在该算法中,return
等同于“返回而不将该 header 添加到 Headers 对象”。
关于javascript - 自定义 header 未添加到 Request 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42506376/