javascript - 如何使用 Javascript 的 OpenWeatherMap API?

标签 javascript weather-api openweathermap

我正在尝试使用 OpenWeatherMap API 为 javascript 创建一个天气应用程序。我的网络应用程序的代码是:

<!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>

我在这里弄错了什么?

最佳答案

您还没有为 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/

关于javascript - 如何使用 Javascript 的 OpenWeatherMap API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32022699/

相关文章:

ios - 使用 Openweathermap 获取名称中包含空格的城市的天气

javascript - Jquery 热键无法使用函数发送值

javascript - 省略 Order to create "average"Google Chart API 中的一些行

api - 如何通过 zip 获取 openweathermap.org 信息?

javascript - getJSON没有输出?

json - Swift - 如何错误检查 JSON 文件?

JSON SWIFT OpenWeatherMapAPI

ios - 如何在 Swift 2.0 中从数组内部的数组中提取 JSON 数据

javascript - 使用javascript创建和修改xml文件

c# - 如何获取上一页的值