0

I am trying to understand how getJSON works by making a few tests on Codepen, such as printing the JSON data on the console. I print the data by using the following:

$.getJSON("https://www.freecodecamp.org/json/cats.json", function(json) {
    $(".message").html(JSON.stringify(json));
});

What I noticed is that with a JSON file such as https://quotesappfree.herokuapp.com/quotes.json the result is printed on the console. However, when I change to this other entry https://www.freecodecamp.org/json/cats.json , it stops working.

Why can I print the JSON file in one case and not in the other? And how would I be able to print this latter case?

Thanks in advance

Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338
A. Barrozo
  • 95
  • 9
  • Is your JavaScript hosted on the same domain as the first JSON file (QuotesAppFree)? Because both JSON files give me CORS 'Access-Control-Allow-Origin' errors. If your same-origin file isn't throwing errros, but the other is, that would be the cause of the problem. – Obsidian Age Oct 11 '17 at 01:08

1 Answers1

1

The reason the second one doesn't work is because freecodecamp is rejecting the request because of CORS

https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS

Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338