0

This is my function:

  $.ajax({
    url: 'http://ec2-52-202-201-59.compute-1.amazonaws.com:8080  /receipts',
    dataType: 'application/json',
    complete: function(data){
        console.log(data)
    },
    success: function(data){
        alert(data)
    }
  });

The function never goes on success, and the object that is included in data is the following:

Object { readyState: 0, getResponseHeader: getResponseHeader(),   getAllResponseHeaders: getAllResponseHeaders(), setRequestHeader: setRequestHeader(), overrideMimeType: overrideMimeType(), statusCode: statusCode(), abort: abort(), state: state(), always: always(), catch: catch(), 8 more… } 

If I go on my browser at that link I can see the JSON files. Similarly, If I go in my console, the GET request is done, and I can see in the response body the JSON objects that I need.

Why it the Ajax call doesn't work?

Pointy
  • 405,095
  • 59
  • 585
  • 614
Marco B
  • 21
  • 2
  • 1
    If you add an error handler as well, you'd find out why. I'm getting *"invalid URL"*, which seems about right – adeneo Sep 16 '17 at 21:36
  • The right URL is: http://ec2-52-202-201-59.compute-1.amazonaws.com:8080/receipts. In the code is ok, I don't know why here spaces got added – Marco B Sep 16 '17 at 21:53
  • The response has no Access-Control-Allow-Origin response header, so your browser is blocking your code from accessing the response. If you check your browser devtools console, you'll see that the browser has logged an error message telling you that https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS. – sideshowbarker Sep 16 '17 at 23:17
  • You can work around this by using `url: 'https://cors-anywhere.herokuapp.com/http://ec2-52-202-201-59.compute-1.amazonaws.com:8080/receipts'` in your code. Try it. And for an explanation of what that does, see the *How to use a CORS proxy to get around “No Access-Control-Allow-Origin header” problems* section of the answer at https://stackoverflow.com/questions/43871637/no-access-control-allow-origin-header-is-present-on-the-requested-resource-whe/43881141#43881141 – sideshowbarker Sep 16 '17 at 23:22

1 Answers1

0

I tried to run your example and I got this:

XMLHttpRequest cannot load http://ec2-52-202-201-59.compute-1.amazonaws.com:8080/receipts. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.

It seems that is a cross-domain problem. Check the configuration in your server.

Amazon has some documentation about it