2
votes

I am trying to get the information from this weather API.
http://api.openweathermap.org/data/2.5/weather?lat=39.607966&lon=-106.354868&APPID={MY KEY}&units=imperial

I tried this:

var $data = $('#weather');
    $.ajax({
        type: 'GET',
        url: 'http://api.openweathermap.org/data/2.5/weather?lat=39.607966&lon=-106.354868&APPID={MY KEY}&units=imperial',
        success: function(data) {
            $.each(data, function(i, weather) {
                $data.append('<li>temp: ' + weather.main.temp + '</li>')
            });
        }
    });

but nothing is printing out on the page did I do something wrong?

my html has has a ul id="weather" in it

1
Have you tried logging out the returned data to see what the data looks like? - Nader Dabit
Can you check your console in developer tools (press F12) to see if any request is being made at all? - Jia Jian Goi
Like Jia Jian says, open your developer tools and do a console.log(data); right before your $each method. The json data will display in the console. You might want to add an error handler to the $ajax method as well. - Fraser Crosbie
It says failed to load resource: net: Err_connection_refused then the open weather link. Then underneath it says xhr failed loading: GET then the link - Guy2
but it works when I put it into the address bar - Guy2

1 Answers

0
votes

OK, so it might have to do with your url having an HTTPS connection rather than an HTTP connection: https://openweathermap.desk.com/customer/portal/questions/8166727-http-to-https. Is your API key based on a PRO or Enterprise account?