0

Admittidly, I've never used CORS before and am not fully sure what I'm doing.

I'm trying to call a Lambda function from a website without having to go through JSONP for cross-origin requests. I tried following this tutorial for enabling CORS through the API gateway, but it doesn't seem to work. I created a GET method, tested that it worked through Postman, and then used the enable CORS option from the action drop down menu. I'm sending the request using AJAX as follows

$.ajax({
        url: myEndpoint,
        type: "GET",
        crossDomain: true,
        dataType: "json",
    })

And in response I'm getting the error

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access"

Does anyone know how to correct this issue?

5
  • 1
    During preflight or during the actual request? Did you read that "However, you must rely on the back end to return the Access-Control-Allow-Origin headers because the integration response is disabled for the proxy integration."? Commented Apr 22, 2017 at 20:17
  • That error is what is logged to the console on chrome. How do I tell if it was in preflight or during the actual request? That line in the documentation confused me a bit because another source I saw had some comment about not needing to edit your lambda service, though I guess that was likely wrong. So do I just need to add the CORS headers to my response in my lambda function? Commented Apr 22, 2017 at 20:29
  • If it helps, my Lambda function is receiving and processing the request, it's just the response that never makes it back to the user. Commented Apr 22, 2017 at 20:38
  • Ignore my previous two comments, adding an "Access-Control-Allow-Origin" header to my lambda response fixed it. Thanks for the help! Commented Apr 22, 2017 at 20:40
  • Sorry was away. Preflight can be detected since it's an OPTIONS request. I guess that the gateway handles all those correctly but rely on the backend to add CORS headers for the actual requests. Commented Apr 22, 2017 at 21:06

1 Answer 1

1

If you are using proxy, then your Lambda integration response must have "Access-Control-Allow-Origin" header.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.