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.

Daniel Edwards
  • 81
  • 1
  • 10
  • Possible duplicate of [API Gateway CORS: no 'Access-Control-Allow-Origin' header](https://stackoverflow.com/questions/35190615/api-gateway-cors-no-access-control-allow-origin-header) – Ravi Ram Jun 11 '19 at 20:54

1 Answers1

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.

Daniel Edwards
  • 81
  • 1
  • 10
  • 2
    Please show an example code snippet to help the next person – Ravi Ram Jun 07 '19 at 18:40
  • 1
    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); – Daniel Edwards Jun 11 '19 at 11:25