[Question] json not working

Hi everyone. for some reason codepen sometimes get the api and sometimes doesn’t. I don’t know if its a problem with codepen or with Open Weather API.


For now I just wanna see the data I get from the website so I could manipulate it later. How can I overcome this problem?

Try inserting a console.log(json); in your code, then use codepen’s console.

It’s option three :wink:

Your first code block doesn’t show anything because you’re trying to extract properties (Object.keys(json.weather[1])) of weather[1], but weather array in your case has only one object (weather[1] doesn’t exist).

Second code block doesn’t show anything because you’re appending the result to an element with a class btn, but your HTML doesn’t have such element.

1 Like

What about $("#info").html(JSON.stringify(json)? Why isn’t codepen showing that. I wrote the syntax correctly and the API address is working.
I’m less worried about the second code block because even If I comment that block and the Object.keys(json.weather[1]) It still doesn’t even show the key:value pairs I should get

I hope I’m being really stupid right now, but I’ve inserted the console.log command into the JS section and played with it’s location a little bit, still nothing happens.

Because

This works:

$("#info").html(JSON.stringify(json));

Even that doesn’t work. I changed the code to only contain this

$.getJSON(“http://api.openweathermap.org/data/2.5/weather?lat=51.51&lon=-0.13&appid=1065f9b5ec2b8693d74325f06462a824”, function(json) {
$("#info").html(JSON.stringify(json))
});

and it’s not working.
I checked if it’s a browser problem(even though I was sure it’s unrelated) and it’s not the case.

I just checked your codepen and the code works.

What exactly is not working for you?

And here it happens again…
I only see the word button instead of the array with the keys values in it. But sometimes it does show the array even though I didn’t change the code. That’s why I wondered if it has something to do with codepen or open weather API.
For some reason now it’s working, but is believe in a few days it will go back to not working. I will print screen when that happens.

It’s an http/https thing.

OpenWeatherMap does not have an https (secure) protocol, and some browsers (at least Chrome) will not load data from an insecure site when you are on a secure connection. Codepen works either way, and I’m not altogether sure how it picks which to use. So you get what seems like a random error half the time.

Whenever it doesn’t work, just reload on http.

1 Like

Indeed that seems to be the problem. For now I’ll work with http but how can I fix it to also work with https?

Pay $180/month for a developer account?

Or just be careful to only share the http:// link

Wow… For real? that sux

How about trying https://crossorigin.me?