Daniel Edwards
Daniel Edwards

Reputation: 101

AWS Lambda - No 'Access-Control-Allow-Origin' header is present on the requested resource

I have AWS lambda function that I am trying to call from my web app using Axios. It keeps giving the error: 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

I am using serverless and I have added the cors:true attribute to my function. I have also added:

            const response = {
                statusCode:200,
                headers: {
                    'Access-Control-Allow-Origin': '*',
                    'Access-Control-Allow-Credentials': true
                },
                body: JSON.stringify({
                    message: 'Ticket received',
                    TableData: data,
                }),
            };
            callback(null, response);

I still can't get this issue resolve. Any help is greatly appreciated.

Upvotes: 1

Views: 2523

Answers (1)

Daniel Edwards
Daniel Edwards

Reputation: 101

I was returning 2 different responses, the one above and an error response. Once I defined the error message to have the headers it then worked fine.

Upvotes: 1

Related Questions