Serverless Framework with AWS cognito generates CORS error

前端 未结 3 1008
别那么骄傲
别那么骄傲 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!

提交回复
热议问题