Need help with Random Quote Machine - XMLHttpRequest

I am trying to call a REST API with JavaScript and XMLHttpRequest.
The URL is: “

This works from the browser window, but when I try to run it as javascript in the browser, it always returns a status of 0
(Even when I substitute the URL with any another URL for a simple GET request - for e.g., I still get the same result.

Here is the code:

(function callRestAPI() {
  var request = new XMLHttpRequest();
  var url = "";

  request.onreadystatechange = function () {
    if (request.readyState === XMLHttpRequest.DONE) {
      if (request.status === 200) {
        alert("The response was: " + request.responseText);
      } else if (request.status === 0) {
        alert("The response was a status code of 0");
  };"GET", url, "false");


I am at a loss on how to figure this out. Any help would be appreciated.

(Note: I get the same result when I run it locally from a file on my computer and also from (
(I am using Firefox 47 and Chrome 51 to test locally)


  • Jay

I’m ony phone right now so can’t test your code. Do you have a server running? Try Python simple server or get http-server from npm
I think that’s how I solved a similar issue before

I tried it with python simple server running. I get the same result (status of 0)

I’m assuming that XMLHttpRequest can be used for any GET request, and most simple GET requests (like getting the page at don’t need to have any headers set.

So why won’t it work? I have a feeling there is something wrong with my javascript code (or the way I’m calling it), but can’t figure it out…

I had sucessfully made your code works by using a different API, and by putting before request.onreadystatechange.

Do you have any obligation to use XMLHttpRequest ? I find using $.ajax or $.getJSON much more simpler

Yes, I can probably try jQuery.

The reason I was using XMLHttpRequest is that I want to learn how to do this in JavaScript.

I was reading up on CORS, since this is the problem I usually get:

These are the automatic request headers for the GET request sent by Firefox:

So, apparently I get:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote
resource at (Reason:
CORS header ‘Access-Control-Allow-Origin’ missing).

Is there any client-side solution to this? (I know others at FCC have made it work using jQuery, but I haven’t come across a working solution ini JavaScript yet)

In fact, you were pretty much right on using XMLHttpRequest, it seems to be pretty standard when it comes to dealing with CORS.

I’ve been searching for this error a lot lately :

Reason: CORS header ‘Access-Control-Allow-Origin’ missing

And I don’t think anything can be done with Codepen concerning this, but if you are using C9 or another live server, you could set this header in the backend. Here are the interesting read I’ve had :

Thanks Mizu.

I’m reading the links above right now. It seems to be a complex topic.
I was also looking at the book “CORS In Action” (, but can’t really afford to buy it right now.

I will get back to this post as and when I learn more and figure out this issue.

In the meantime, I’m going to try it using a jQuery AJAX request. Interesting how it works using jQuery but not JavaScript. The jQuery library must have some special code under the hood to handle CORS maybe ?

For the time being, I used the following solution to make it work in JavaScript using XMLHttpRequest :

I am using a proxy, so I changed the URL from “” to “

i.e. essentially, now my request is going to “” with my original url as a get request parameter. seems to be some sort of proxy server (don’t really understand how it is doing its magic)

Here are some additional links I found useful on the topic of “Cross Domain Scripting” :

  1. Ways to circumvent the “Same-Origin Policy” :
  2. Cross-Domain requests in Javascript :
  3. JSON Proxy :
  4. Using CORS :
  5. Making Cross-Domain requests with CORS :
  6. Successful Cross Origin Resource Sharing (CORS) Using A Proxy Server :

Hope the above links help other people with these issues too!

1 Like

Thanks @jaytsecan! I’ve been hacking away at this very same issue for HOURS!