1

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.

1

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Please show an example code snippet to help the next person
const response = { statusCode:500, headers: { "Access-Control-Allow-Origin" : "*", // Required for CORS support to work "Access-Control-Allow-Credentials" : true }, body: JSON.stringify({ message: 'Failed to add ticket to the database.', error: err }), }; callback(null, response);

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.