Javascript, & HTML Page

Hi everyone. I’m hoping some of you Javascript and HTML gurus can help me out.

I’m trying to retrieve data from the DarkSky API using Javascript, which I am successful at using the following code in the code section of my project.

var getdarksky = new XMLHttpRequest();
getdarksky.open(‘GET’,“https://api.darksky.net/forecast/myAPIKey/48.197750,-114.313461”, true);
getdarksky.send();

    getdarksky.addEventListener("readystatechange", processRequest, false);

    function processRequest(e) {
        if (getdarksky.readyState == 4 && getdarksky.status == 200) {
            var response = JSON.parse(getdarksky.responseText);
        }
    }

What I’m trying to do with the data that I get returned from this code is load it into an HTML page (or a text stack perhaps?)

I’ve tried using a

class thusly:
</span>

I know the data is being returned because my API call count goes up when I try and preview the page.

Any help is appreciated

The response you get will be a JavaScript object. That’s what json.parse() returns.

So you have to write the part that takes that and puts it into your html page.

This should get you started: https://www.w3schools.com/js/tryit.asp?filename=tryjson_parse

1 Like

I looked at the example you provided and tried to duplicate it in my project but it still didn’t work. Here’s the code that I’m using now.

<body>

<p id="getdarksky"><p>


<script>
var getdarksky = new XMLHttpRequest();

       getdarksky.open('GET',"https://api.darksky.net/forecast/
       myAPIKEY/48.197750,-114.313461", true);
       getdarksky.send();

      getdarksky.addEventListener("readystatechange", processRequest, false);

 function processRequest(e) {
     if (getdarksky.readyState == 4 && getdarksky.status == 200) {
        var response = JSON.parse(getdarksky.responseText);
    }
}
getdarksky.getElementById("response")
</script>
 </body>

If you could tell me what I’m doing wrong here I would appreciate it. Thanks!

Look at your id set above and the one in “.getElementByID”…

They need to match and that bottom one needs to be document.getElementById…

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.