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

1
  • 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. Commented Oct 11, 2017 at 1:08

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.