问题
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
Headersobject (headers), run these steps:
- 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