JQuery - Problema con $.getJSON

 
Vista:

Problema con $.getJSON

Publicado por Volatil (1 intervención) el 14/05/2015 17:29:11
Por más vueltas que le doy, no consigo comprender porque no me muestra la temperatura de este JSON.

Si pongo solo lo siguiente en el archivo javascript, si me muestra la temperatura:

1
2
3
$.getJSON("http://api.openweathermap.org/data/2.5/weather?q=" + "Cadiz" + ",es&units=metric", function(json){
    				alert("Temperatura: " + json.main.temp);
			  		});

Si quito el código anterior y pongo este pues no me muestra la temperatura, ¿Alguien sabe por qué?
1
2
3
4
5
6
$("#btnTiempoActual").click(function(){
	alert("Mensaje de prueba.");
	$.getJSON("http://api.openweathermap.org/data/2.5/weather?q=" + "Cadiz" + ",es&units=metric", function(json){
    	alert("Temperatura: " + json.main.temp);
	});
 });
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil

Problema con $.getJSON

Publicado por Maxi (1 intervención) el 17/05/2015 10:20:14
Probé tu ejemplo y funciona. ¿Tienes tu código dentro de un $(document).ready? ¿Tienes un botón con el id btnTiempoActual?

Aquí dejo el mismo código que tu tienes en forma completa para que puedas comprobar que realmente funciona:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<!DOCTYPE html>
<html>
<head>
    <title>Temperatura</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
    <script>
        $(document).ready(function () {
            $("#btnTiempoActual").click(function(){
                alert("Mensaje de prueba.");
                $.getJSON("http://api.openweathermap.org/data/2.5/weather?q=" + "Cadiz" + ",es&units=metric", function(json) {
                    alert("Temperatura: " + json.main.temp);
                });
            });
        });
    </script>
</head>
<body>
    <a id="btnTiempoActual" href="#">Ver temperatura</a>
</body>
</html>
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar