使用不记名令牌的 React API 调用已被 CORS 策略阻止
React API call with bearer token has been blocked by CORS policy
我正在尝试使用不记名令牌进行 API 调用以获取数据。但是有一个错误Access to fetch at 'https://MYAPI..' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the请求的资源。如果不透明响应满足您的需求,请将请求的模式设置为 'no-cors' 以在禁用 CORS 的情况下获取资源。另外,我的令牌为空。我尝试从 localStorage 获取。local storage token example 这是我的令牌。谁能告诉我我的问题在哪里?谢谢
const [user, setUsers] = React.useState([]);
var token = localStorage.getItem("accessToken");
React.useEffect(()=>{
fetch(URLS.GetAllUsers,{
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + token,
}})
.then(res=> res.json())
.then((response) => {
setUsers(response.displayName)
})
.catch((error) => console.log(error));
});
我认为你必须在服务器内部启用 CORS。
我正在尝试使用不记名令牌进行 API 调用以获取数据。但是有一个错误Access to fetch at 'https://MYAPI..' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the请求的资源。如果不透明响应满足您的需求,请将请求的模式设置为 'no-cors' 以在禁用 CORS 的情况下获取资源。另外,我的令牌为空。我尝试从 localStorage 获取。local storage token example 这是我的令牌。谁能告诉我我的问题在哪里?谢谢
const [user, setUsers] = React.useState([]);
var token = localStorage.getItem("accessToken");
React.useEffect(()=>{
fetch(URLS.GetAllUsers,{
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + token,
}})
.then(res=> res.json())
.then((response) => {
setUsers(response.displayName)
})
.catch((error) => console.log(error));
});
我认为你必须在服务器内部启用 CORS。