使用 fetch() 从带有不记名的 api 接收数据不起作用 (401)

问题描述 投票:0回答:1

我正在尝试通过这样做从 api 获取数据:

  const url = `https://......`

  console.log('DEBUG TOKEN', `Bearer ${token}`) // is correct
  const response = await fetch(url, {
    method: 'POST',
    mode: 'no-cors',
    headers: {
      'Content-Type': 'application/json',
      Authorization: `Bearer ${token}`,
    },
    body: JSON.stringify(postString),
  })

问题是,它返回 401(未经授权)错误。 如果我使用curl执行相同的请求,它会起作用:

curl -X POST \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer ........asdfasdf....." \
  -d '{"my":"json","data":"here"}' \
  "https://......"

我对 fetch() 和curl 使用相同的有效负载、令牌和地址。 我的 fetch() 代码有什么问题吗?

非常感谢您的帮助!

干杯

typescript fetch-api
1个回答
0
投票

没关系...

问题是

    mode: 'no-cors',

据我了解,它也删除了身份验证标头。没有它,它也能工作。

干杯

© www.soinside.com 2019 - 2024. All rights reserved.