1

I have a simple Lambda function that takes in some text, and returns that text as a response. I hooked it up with AWS API Gateway and tested it out in their console and also on Postman. I enabled CORS, and the headers on Postman seem to be right, the Access-Control-Allow-Origin is set to *.

Here's the Postman result:

enter image description here

I couldn't get it working on my local, so I decided to host a static page here:

https://smileyfacetest.firebaseapp.com/

test = {"text": ":)"}
$.post( "https://pq8thdrp0a.execute-api.us-west-2.amazonaws.com/dev", test)
    .done(function( data ) {
       console.log(data);
    });

But still got an error of:

XMLHttpRequest cannot load https://pq8thdrp0a.execute-api.us-west-2.amazonaws.com/dev. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://smileyfacetest.firebaseapp.com' is therefore not allowed access. The response had HTTP status code 400.

Is there anything I'm doing wrong on the frontend? If it's working on Postman, and I have CORS enabled on the endpoint for the API, I don't see why it should be throwing an error. Any help would be really appreciated!

Here's the API endpoint if you want to try it in Postman:

https://pq8thdrp0a.execute-api.us-west-2.amazonaws.com/dev

1 Answer 1

3

I tested your call and it works. However, you need to set the content-type to JSON in the jQuery call:

$.ajax({
    type: 'POST',
    url: '/form/',
    data: '{"name":"jonas"}', // or JSON.stringify ({name: 'jonas'}),
    success: function(data) { alert('data: ' + data); },
    contentType: "application/json",
    dataType: 'json'
});

I took this snippet from How can I use JQuery to post JSON data?

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.