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
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