Using Fetch with Authorization Header and CORS

允我心安 提交于 2019-12-08 19:46:36

问题


I'm trying to get my request to go through to a online game API that I can't seem to get working. I'm using the Fetch API, and some request require Authorization Bearer token, but the request never gets sent with the authorization header.

I have tried

mode: 'no-cors',
credentials: 'include'

and obviously putting the Authorization in the header like so

header: { 'Authorization': 'Bearer TOKEN' }

but the request still does not go with the authorization. Can anyone point me in the right direction?

EDIT Heres the way I am making the request

fetch(URL, {
  credentials: 'include',
  header: {
    'Authorization': 'Bearer TOKEN'
  }
})

回答1:


Read the documentation

Keys can be passed either via query parameter or HTTP header. Guildwars API servers do not support preflighted CORS requests, so if your application is running in the user's browser you'll need to use the query parameter.

To pass via query parameter, include "?access_token=" in your request.




回答2:


The key name should be headers, not header.

fetch(URL, {
  credentials: 'include',
  headers: {
    'Authorization': 'Bearer TOKEN'
  }
})


来源:https://stackoverflow.com/questions/49967188/using-fetch-with-authorization-header-and-cors

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