Why does not the browser show me anything?

2

Hi, I am learning the operation of JSON with javascript consuming data from a weather api, but when I run it, it shows me the browser in blank. This example I took it from here, I wanted to know what I'm failing that I can not run the example, thanks. link

<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"> </script>
</head>
<body>
<script>
var vars;
var temp_c;
var temp_f;

$.ajax({
type: "GET",
url: "http://api.openweathermap.org/data/2.5/weather?q=London, uk",
dataType: "json",
success: function (data) {
vars = data.main;
temp_c = vars.temp - 273.15;
temp_f = 1.8 * (vars.temp - 273.15) + 32;
},
error: function (jqXHR, textStatus, errorThrown) {
alert(errorThrown);
}
});
</script>

</body>
</html>
    
asked by Diego 22.04.2016 в 01:36
source

3 answers

1

Initially I thought that the problem was that you were missing the API because when I go to the AJAX URL I receive the following message:

{"cod":401, "message": "Invalid API key. Please see http://openweathermap.org/faq#error401 for more info."}

But there is something else. It seems to me that you are not loading the jQuery library properly, because when I load it, I receive an alert message with "Unauthorized". Not having a protocol could be a problem if you run the page from local (with the protocol file:// ), try to change the link to the jQuery so that it has the protocol http:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"> </script>

Now it should work for you (or at least you should receive the "Unauthorized" message because you would still be missing the API key)

    
answered by 22.04.2016 / 02:01
source
0

The problem is that you do not have a valid API Key to access this service.

the call should be:

http://api.openweathermap.org/data/2.5/forecast/city?id=524901&APPID={APIKEY}

check how to get the Key API

link

Also remember to add the http: // protocol to access the jquery script:

  

src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js" >

    
answered by 22.04.2016 в 01:50
-1

Missing it like this:

<script src ="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

In addition, you must have your key id, which you get by signing up on the page.

    
answered by 31.05.2017 в 22:11