2

I have a Lambda function on AWS and it's written in Python 3.6 I also have my API deployed and the CORS enabled to all of my resources When I test the POST from AWS API gateway it works fine and the same Success I get when I try it from Postman but when I try it from The html hosted in my S3 bucket it does not work! I try to make the post request like so:

$.ajax({
  type: 'POST',
  url: 'https://xxxxxxx.execute-api.us-east-1.amazonaws.com/dev/myapp',
  data: '{"fName":"jonas","lName": "Hoda", " Email":"[email protected]", "Age": 45, "Hight": 190, "Income": 120000}', 
  success: function(data) { alert('data: ' + data); },
  contentType: "application/json",
  dataType: 'json'
});

But I keep getting the same response: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://app.my-real-domain.com' is therefore not allowed access. The response had HTTP status code 400.

What else do I have to do? Do I need to edit something in my API or at my Python function? Pleases help me fix this Thanks in advance ...

6
  • It's saying that you don't appear to have CORS enabled. Did you redeploy the API after enabling CORS? Commented Dec 8, 2017 at 17:53
  • Thanks for the response, yes of course I re-deployed after enabling the CORS ...actually I re-deployed many time after and I check the headers every time to make sure the CORS are enabled and everything is OK but still the same error Commented Dec 8, 2017 at 18:14
  • Are you using Lambda Proxy integration? Commented Dec 9, 2017 at 0:22
  • @dashmug thanks for your response, and no I do not use Lambda Proxy integration Commented Dec 9, 2017 at 4:21
  • @AboJoTheGreat Great. In your mapping templates, make sure you return the correct CORS headers. Commented Dec 9, 2017 at 20:43

2 Answers 2

1

OK I found the solution! First I tried all the ideas I got here and nothing worked Actually most of them were already applied in my project So I tried to see if the problem is in the API or is it in The request so I disabled the Request Validator in the API-Gateway and suddenly it worked! The post request went just fine with no errors and the lambda At the back-end worked, stored the data at the Dynamo DB and Returned the right response so I edited my Model schema at API-Gateway and removed the 'required' part and applied it again And everything is OK now! I do not know why this solved the issue specially that I reviewed The parameters tens of times and it's being passed using the right Names and types .. Anyway I just wanted to share this if anyone else has this problem And thanks for anyone tried to help me...

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

1 Comment

I'm having exactly the same problem. Like you, I have been through most suggestions and had most already applied. I have an API rule set up in Cloudfront... Where is this request validator of which you speak? Can you remember?
0

CORS doesn't seem to be setup on your resource.

There is a wizard on the console that helps you setup CORS on a resource. The documentation also has a detailed guide http://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-cors.html on how to setup CORS.

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.