繁体   English   中英

请求的资源 API 网关上不存在“Access-Control-Allow-Origin”header

[英]No 'Access-Control-Allow-Origin' header is present on the requested resource API Gateway

I have an API Gateway set in AWS that is connected to lambda function, and also I have a S3 website that is trying to call the endpoints exposed by the API Gateway, CORS are enabled for S3 website but when I try to hit any of the我收到此错误的端点:

Access to XMLHttpRequest at 'https://myapi.amazonaws.com/' from origin 'http://mywebsite.region.amazonaws.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

我已经在 S3 网站中设置了权限和 cors 但看起来不起作用,这是我的配置:

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
    <AllowedOrigin>http://mywebsite.region.amazonaws.com</AllowedOrigin>
    <AllowedOrigin>*</AllowedOrigin>
    <AllowedMethod>GET</AllowedMethod>
    <AllowedMethod>POST</AllowedMethod>
    <MaxAgeSeconds>30000</MaxAgeSeconds>
    <AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>

我也尝试<AllowedOrigin>*</AllowedOrigin>但没有奏效,过去有没有人面对并解决过这个问题?

So after a good research, I follow this guide to enable cors in my API Gateway and is working now and also I had to change the integration between the methods and the lambda function, I was using lambda proxy integration and looks like that config gives problems (这里缺乏经验)但现在工作正常,谢谢大家的评论!

更新

I had another issue when I tried to the first solution that I posted here, then I go back to Proxy Integration Lambda and continue with my research, I had to apply more changes to the lambda function that expose the GraphQL Server

这是它现在的样子:

const server = new ApolloServer({
    typeDefs,
    resolvers,
    playground: {
        endpoint: "/api/graphql"
    },
    context: ({ event, context }) => ({
        headers: event.headers,
        functionName: context.functionName,
        event,
        context,
    }),
});

exports.graphqlHandler = server.createHandler({
    cors: {
        origin: '*',
        credentials: true,
    },
});

使用该配置,我的服务器工作正常,没有任何CORS问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM