Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Thom Benjamin
Thom Benjamin
10,494 Points

Origin Null when fetching data with XMLHttpRequest

I'm trying to fetch some data from a public API. When I open the URL in the browser I get the needed JSON, but when fetching it I get this ERROR.

[Error] Cross-origin redirection to https://sv443.net/jokeapi/category/Any denied by Cross-Origin Resource Sharing policy: Origin null is not allowed by Access-Control-Allow-Origin.

This is my code:

let jokeUrl = "http://sv443.net/jokeapi/category/Any";

let xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
  if (xhr.status == 200) {
    console.log(xhr.responseText);
  } else {
    console.log("There was an error: " + xhr.status);
  };
};
xhr.open('GET', jokeUrl, true);
xhr.send();

1 Answer

I was able to get a response by changing the url from http to https.

Thom Benjamin
Thom Benjamin
10,494 Points

Thanks! Do you also know why this is happening?