如何在Javascript中使用OpenWeatherMap API?

7

我正在尝试使用OpenWeatherMap API为JavaScript创建天气应用程序。我的Web应用程序的代码如下:

<!DOCTYPE html>
<html> 
    <head>
        <title>Weather</title>
        <script src = "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.2.min.js"></script>
        <script>
            function gettingJSON(){
                document.write("jquery loaded");
                $.getJSON("api.openweathermap.org/data/2.5/weather?q=London&APPID=ee6596241130f193adf1ba90e625cc10",function(json){
                document.write(json);
            }
        </script>
    </head>
    <body>
        <button id = "getIt" onclick = "gettingJSON()">Get JSON</button>
    </body>
</html>

这里我错在哪里了?

你的脚本末尾缺少 );}。 - Bertrand Martel
1个回答

9
您没有为getJSON方法完成括号。除此之外,我对您的代码进行了一些修改。
<!DOCTYPE html>
<html>
<head>
<title>Weather</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
    function gettingJSON(){
        document.write("jquery loaded");
        $.getJSON("http://api.openweathermap.org/data/2.5/weather?q=London&APPID=ee6596241130f193adf1ba90e625cc10",function(json){
            document.write(JSON.stringify(json));
        });
    }
    </script>
</head>
<body>
<button id = "getIt" onclick = "gettingJSON()">Get JSON</button>
</body>
</html>

http://jsfiddle.net/kqLeh3mz/


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接