custom headers are not added to Request object

拥有回忆 提交于 2019-11-28 09:03:47

问题


I'm trying to use fetch api.

First i create a new Headers() object:

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"}

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',
});

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

If i try to fetch(oReq) i get 401 unauthorized response.

What makes the headers disappear?


回答1:


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:

To append a name/value (name/value) pair to a Headers object (headers), run these steps:

  1. Otherwise, if guard is "request-no-cors" and name/value is not a CORS-safelisted request-header, return.

In that algorithm, return equates to “return without adding that header to the Headers object”.



来源:https://stackoverflow.com/questions/42506376/custom-headers-are-not-added-to-request-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!