自定义标头不会添加到Request对象 [英] custom headers are not added to Request object
问题描述
我正在尝试使用 fetch
api。
I'm trying to use fetch
api.
首先我创建一个新的Headers()
对象:
var oHeaders = new Headers({
'Accept': 'application/json',
'Content-Type': 'application/json',
"X-DocuSign-Authentication": '{"Username":"xxx","Password":"xxx","IntegratorKey":"xxx"}'
})
如果我尝试记录标题,那么在实例化标题之后,一切都是正确的。
After headers is instantiated if i try to log headers everything is correct.
oHeaders.forEach(function(v){console.log(v)})
//logs: 2 application/json {"Username":"xxx","Password":"xxx","IntegratorKey":"xxx"}
我创建了Request对象:
the i create the Request object:
var oReq = new Request('https://eu.docusign.net/restapi/v2/login_information', {
method: 'GET',
headers: oHeaders,
mode: 'no-cors',
});
如果我尝试仅记录请求对象的标题 accept
标题将在那里。
If i try to log the headers of the request object only the accept
header will be there.
oReq.headers.forEach(function(v){console.log(v)})
//logs: application/json
如果我尝试 fetch(oReq)
我得到 401未经授权的
回复。
If i try to fetch(oReq)
i get 401 unauthorized
response.
什么使标题消失?
推荐答案
设置模式时:'no-cors'
对于请求,浏览器不允许您设置除 CORS-safelisted请求标头。请参阅规范要求:
When you set mode: 'no-cors'
for a request, browsers won’t allow you to set any request headers other than CORS-safelisted request-headers. See the spec requirements:
将名称/值(名称 / 值)对附加到
标题
object( headers ),运行以下步骤:
To append a name/value (name/value) pair to a
Headers
object (headers), run these steps:
- 否则,如果 guard 是
request-no-cors
而名称 / 值不是 CORS-safelisted请求标题,返回。
- Otherwise, if guard is "
request-no-cors
" and name/value is not a CORS-safelisted request-header, return.
在该算法中, return
等于返回将该标题添加到Headers对象。
In that algorithm, return
equates to "return without adding that header to the Headers object".
这篇关于自定义标头不会添加到Request对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!