[Question] json not working

[Question] json not working
0

#1

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?


#2

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


#3

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.


#4

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


#5

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.


#6

Because

This works:

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

#7

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.


#8

I just checked your codepen and the code works.

What exactly is not working for you?


#9

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.


#10

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.


#11

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?


#12

Pay $180/month for a developer account?

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


#13

Wow… For real? that sux


#14

How about trying https://crossorigin.me?