如何解决对于空闲的chrome会话,CORS策略已阻止从源<localhost>到<URL>的获取访问
我的React应用上的Chrome标签页/会话闲置后,出现奇怪的错误。我正在使用Apollo GraphQL Client。
我还没有精确计时,但是在页面上坐了30分钟左右之后,如果我要单击以切换到一条路线,并且会尝试从服务者那里获取一些数据,我会收到此错误:
Access to fetch at 'https://xxxxxxxxx.execute-api.xxxxxxxxx.amazonaws.com/nonprod/graphql' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs,set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
刷新页面后,它可以正常工作。
下面是我的ApolloWrapper的全部内容:
const uploadLink = createUploadLink({
uri: process.env.REACT_APP_GRAPHQL_ENDPOINT_URL,});
const errorLink = onError(({ graphQLErrors,networkError }) => {
if (graphQLErrors)
graphQLErrors.map(({ message,locations,path }) =>
console.log(
`[GraphQL error]: Message: ${message},Location: ${
typeof locations === 'object' ? JSON.stringify(locations) : locations
},Path: ${path}`,),);
if (networkError) console.log(`[Network error]: ${networkError}`);
});
const ApolloWrapper = ({ children }) => {
const { session,logout,refreshSession } = useContext(AuthContext);
const retryLink = new RetryLink({
delay: {
initial: 0,},attempts: {
max: 2,retryIf: async (err) => {
// when unauthorize response from server,refresh id token
if ([401,403].includes(err.statusCode)) {
try {
await refreshSession();
} catch (error) {
logout();
return false;
}
return true;
}
return true;
},});
const authLink = setContext((_,{ headers }) => {
// get the authentication token from local storage if it exists
const token = session.idToken.jwtToken;
// return the headers to the context so httpLink can read them
return {
headers: {
...headers,Authorization: token ? `${token}` : '',};
});
const client = new ApolloClient({
link: errorLink.concat(retryLink).concat(authLink).concat(uploadLink),cache: new InMemoryCache(),});
return <ApolloProvider client={client}>{children}</ApolloProvider>;
};
export default ApolloWrapper;
我以为这可能是我的令牌到期了,但是我正在刷新它,所以我认为那里没有发生任何奇怪的事情。
查看Chrome DevTools,它没有给我任何形式的HTTP响应代码,而且我什至没有碰到后端,因为我的CloudWatch日志没有显示任何内容。
可能是什么原因造成的?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。