Getting a .status code 0 from an HTTP request

I’m trying to make a request to the Darksky API to show the local weather (a freeCodeCamp project).

To get the response from the server, I’m following the Getting Started with Ajax guide from Mozilla.
As the response of a request from DarkSky is in JSON (if I’m not wrong. click here for more information), I diced to use the first example of the Mozilla’s guide. The basic code from the article is shown below. In this example, the code is ‘GETting’ the information from a local file, ‘test.html’:

Make a request
<script>
    (function() {
        var httpRequest;
        document.getElementById("ajaxButton").addEventListener('click', makeRequest);

        function makeRequest() {
            httpRequest = new XMLHttpRequest();

            if (!httpRequest) {
                alert('Giving up :( Cannot create an XMLHTTP instance');
                return false;
            }
            httpRequest.onreadystatechange = alertContents;
            httpRequest.open('GET', 'test.html');
            httpRequest.send();
        }

        function alertContents() {
            if (httpRequest.readyState === XMLHttpRequest.DONE) {
                if (httpRequest.status === 200) {
                    alert(httpRequest.responseText);
                } else {
                    alert('There was a problem with the request.' + httpRequest.status);
                }
            }
        }
    }) ();
</script>

Trying to get my response from DarkSky, a just changed the httpRequest.open('GET', 'test.html'); to httpRequest.open('GET', 'https://api.darksky.net/forecast/my_personal_key/0.0000,-111.1111');. But I got a status code id 0 from my http request.

Making a search, I discovered that there is no code 0 from an http request and that W3C address some possibilities to this code. This answer and that has more information about that.

I’m trying to do that in a local file; using WebStorm IDE (updated); Chrome as a browser; and from Brazil.

Could someone help me get the correct answer from the DarkSky API using the vanilla JavaScript (no jQuery, please).

Thank you for your interest in help me.

As far as I know, there is no status cod 0 in http responses, so it’s definitely a Dark Sky error of some sort.

You should check their documentation for further assistance.
So far I’ll check:

1 - Do you have a valid key?
2 - Are you having some errors in console (like CORS or No
-Header)

1 Like
  1. Yes. When I use the link - with my key and coordinates - in my browser, it seems that the browser gives me the information.

  2. Yes. the following error:

XMLHttpRequest cannot load How Dark Sky users can use the Apple Weather app - Apple Support. No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin ‘http://localhost:63342’ is therefore not allowed access.

Sorry, but I didn’t check the console for error. :open_mouth: Thank you very much for your help. :man_dancing: I can’t check it now, but tomorrow I’ll work on this problem. I’ll read the CORS guide on Mozilla and update this post. I was stuck on this problem for some days. :blush:

Simply parse the request through crossorigin.me and you’ll (hopefully) be ready to go.

Basically change the url request from this:
https://api.darksky.net/forecast/my_personal_key/0.0000,-111.1111

to this:
https://crossorigin.me/https://api.darksky.net/forecast/my_personal_key/0.0000,-111.1111

and let them handle the CORS request for you.

2 Likes

It Worked. :smile: Thank you very much for your help. I’ll try to learn more about CORS too, but now I can go on with my project. :weight_lifting_man: