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

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
Mike
  • 2,633
  • 6
  • 31
  • 41

1 Answers1

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?

Community
  • 1
  • 1
Luc Hendriks
  • 2,473
  • 13
  • 18