Serverless Framework with AWS cognito generates CORS error

前端 未结 3 994
别那么骄傲
别那么骄傲 2020-12-12 04:42

I get this error message from the Angular frontend and I am not authorized to touch my lambda code:

`Access to fetch at \'https://testapicd.***.***.com/local         


        
相关标签:
3条回答
  • 2020-12-12 05:15

    I have wrestled with this problem for hours (if not days) before and it turned out not only I had to enable cors on the serverless.yml file but also add the response headers as attributes in the object you return from your Lambda.

    Something like this should do it:

    const response = {
        statusCode: 200,
        headers: {
          'Access-Control-Allow-Origin': '*',
          'Access-Control-Allow-Credentials': true,
        },
        body: JSON.stringify({
          product: product
        }),
      };
    

    This article saved my life back then and I hope it saves yours!

    0 讨论(0)
  • 2020-12-12 05:17

    The CORS error is thrown when you are making requests to servers in other domains. Depending on the server you are using to host the angular code there are many ways you can solve this.

    You can try this google chrome extension to see if you can effectivelly fix the problem by ignoring the CORS errors.

    One of the most commons ways to solve this problem is to configure a proxy server, but you can also solve it by whitelisting your test domain manipulating the header Access-Control-Allow-Origin in your requests.

    0 讨论(0)
  • 2020-12-12 05:20

    This line in serverless.yml

    arn: ${file(config.${self:provider.stage}.json):userpoolarn}

    Should have been

    arn: ${file(config.${opt:stage}.json):userpoolarn}

    0 讨论(0)
提交回复
热议问题