我正在尝试通过这样做从 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() 代码有什么问题吗?
非常感谢您的帮助!
干杯
没关系...
问题是
mode: 'no-cors',
据我了解,它也删除了身份验证标头。没有它,它也能工作。
干杯