Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 在没有授权标头的情况下发送React请求_Reactjs_Http Headers_Authorization_Bearer Token_Fastify - Fatal编程技术网

Reactjs 在没有授权标头的情况下发送React请求

Reactjs 在没有授权标头的情况下发送React请求,reactjs,http-headers,authorization,bearer-token,fastify,Reactjs,Http Headers,Authorization,Bearer Token,Fastify,我试图在我的头中向fastify http服务器传递一个承载令牌 我将请求中的标题设置为: ... const headers = { Accept: 'application/json', 'Content-Type': 'application/json' } const token = localStorage.getItem('token') if (token) { headers['Authorization'] = `Bearer ${token}` } const n

我试图在我的头中向fastify http服务器传递一个承载令牌

我将请求中的标题设置为:

...
const headers = {
  Accept: 'application/json',
  'Content-Type': 'application/json'
}
const token = localStorage.getItem('token')
if (token) {
  headers['Authorization'] = `Bearer ${token}`
}
const newOptions = {
  ...options,
  mode: 'no-cors',
  headers
}
console.log('options:', newOptions)
return fetch(url, newOptions)
My console.log打印:

options: {
  mode: "no-cors", 
  headers: {
    Accept: "application/json",
    Content-Type: "application/json",
    Authorization: "Bearer NQ9xQLmYtq92aT8JHHRd7DGZJ..."
  }
}
从Chrome的网络选项卡中,我查看了标题,
Authorization
只是不存在。我的路由处理程序函数如下所示:

async function user(server, options) {
  server.route({
    method: 'GET',
    url: '/user/:email',
    handler: async (req, res) => {
      const username = req.params.email
      console.log('user email:', username)
      console.log('headers:', req.headers)
      res.send({
        type: 'promoter'
      })
    }
  })
}
当我在服务器上打印标题时,它也没有
授权
,显示:

headers: { host: 'localhost:5000',
  connection: 'keep-alive',
  pragma: 'no-cache',
  'cache-control': 'no-cache',
  accept: 'application/json',
  'sec-fetch-dest': 'empty',
  'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.106 Safari/537.36',
  'sec-fetch-site': 'same-site',
  'sec-fetch-mode': 'no-cors',
  referer: 'http://localhost:3000/admin',
  'accept-encoding': 'gzip, deflate, br',
  'accept-language': 'en-US,en;q=0.9,ru;q=0.8' }
我错过了什么

另一个有趣的问题是,当我从
Postman
运行请求时,它会显示200个响应代码,fastify会将
200
打印到日志中。但是,从saga/request运行时:

  return fetch(url, newOptions)
    .then(checkStatus)
    .then(parseJSON)

我得到了
响应。在请求方法中,状态是
0
而不是
200
,而服务器日志仍然显示
“res”:{“statusCode”:200}

尝试添加
with credentials:true
credentials:'include'

选项:{
模式:“无cors”,

使用凭证:true,//我发现了问题所在


显然,我的Chrome版本是80.0.3987.106(官方版本)(64位)
以及可能的其他浏览器和Chrome的旧版本,当它与
无cors
模式一起使用时,去除
授权
标题。启用
cors
并设置适当的标题可以解决问题。

不幸的是,没有区别。我想知道是否有可能hat fastify以某种方式禁用了授权标头,在协商期间将其禁用。我已经看到,在某些服务器上,授权标头被服务器配置设置阻止。我不熟悉fastify,尽管您仍然应该在开发工具的“网络”选项卡中看到标头正在传递。它是否发送选项飞行前请求?可能这是CORS问题。我启用了
无CORS
模式,并将请求发送到另一个端口。我认为
CORS
不太可能是候选端口,但可能是。感谢您帮助解决此问题。非常感谢。如果您显示添加的标题,此解决方案将更有用:)