【发布时间】:2020-04-03 22:21:34
【问题描述】:
这里是代码
fetch('http://localhost:3000/tasks/', {
method: 'GET',
mode: 'no-cors',
headers: new Headers({
'Authorization':
'Bearer <jwt_token>'
}),
redirect: 'follow'
})
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));
令牌正在工作。我使用 Postman、php 中的 curl 和 nodeJS 对其进行了测试,一切正常。我总是得到回应。但使用这是我的反应应用程序,我得到的只是错误 401,这意味着我未经授权。在devtools的Networks选项卡中查看请求后,发现没有Authorization。
请求标头:
GET /tasks/ HTTP/1.1
Host: localhost:3000
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36
Sec-Fetch-Dest: empty
Accept: */*
Sec-Fetch-Site: same-site
Sec-Fetch-Mode: no-cors
Referer: http://localhost:3001/
Accept-Encoding: gzip, deflate, br
Accept-Language: en-GB,en;q=0.9,ja-GB;q=0.8,ja;q=0.7,en-US;q=0.6
我的获取代码有问题吗?还是反应有问题?我不知道。请发送帮助。
【问题讨论】:
-
因为“no-cors”
标签: javascript reactjs