【问题标题】:OpenWeather: not getting dataOpenWeather:没有获取数据
【发布时间】:2016-06-17 09:13:33
【问题描述】:

我正在尝试获取过去 7 天的天气信息,但它没有返回任何内容。当我从浏览器输入 url 时,它正在工作并返回 xml 格式代码。但是当我用 jquery 代码运行我的 html 时,它没有执行。你能告诉我哪里出错了吗?

我的代码是:

<!doctype html>

<html>

<head>

    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">

    <meta charset="utf-8">

    <title>OpenWeatherMap API jQuery Plugin</title>

    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

    <style>

        /* for presentation only */

        body {
            font: 16px Arial, Helvetica, sans-serif;
            margin: 0;
        }

        .weather-wrapper {
            background: skyblue;
            margin: 5% 0 5% 5%;
            padding: 40px 5%;
            float: left;
            color: white;
            width: 70%;
            max-width: 400px;
        }

        strong {
            color: steelblue;
        }

        .capitalize {
            text-transform: capitalize;
        }

        .hide {
            display: none;
        }

    </style>

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script type="application/javascript">

        $(document).ready(function(){

            $.getJSON('http://api.openweathermap.org/data/2.5/forecast/daily?q=Hyderabad,IN&mode=xml&appid=c9d49310f8023ee2617a7634de23c2aa',function(result){
             console.log(result);

             });


        });
    </script>

</head>

<body lang="en">

<script type="text/javascript">
    if (typeof jQuery == 'undefined') {
        document.write(unescape("%3Cscript src='js/lib/jquery.1.9.1.min.js' type='text/javascript'%3E%3C/script%3E"));
    }


</script>

<script src="js/plugins/openWeather.min.js"></script>


</body>

</html>

【问题讨论】:

  • 那么您是在尝试使用 getJSON 函数获取 XML 数据吗?这似乎是个问题

标签: jquery html json api openweathermap


【解决方案1】:

getJSON 函数期望返回 JSON 数据。所以你需要一个可以像这样返回 XML 数据的函数:

$.ajax({
    type: "get",
    url: "http://api.openweathermap.org/data/2.5/forecast/daily?q=Hyderabad,IN&mode=xml&appid=c9d49310f8023ee2617a7634de23c2aa",
    dataType: "xml",
    success: function(data) {
        /* handle data here */
    },
    error: function(xhr, status) {
        /* handle error here */
    }
});

现在这个函数可以返回xml数据(dataType: "xml"

您还可以更改 url 以返回 JSON 数据: http://api.openweathermap.org/data/2.5/forecast/daily?q=Hyderabad,IN&amp;mode=json&amp;appid=c9d49310f8023ee2617a7634de23c2aa

我在网址中更改了mode=json

【讨论】:

  • 是否需要调用上述方法,例如单击按钮之类的?只是我复制了上面的代码并在$(document).ready(function(){ }); 中过去并运行。但它没有被执行。你能完整地发布答案吗?
  • 它正在执行,但您需要对 succes 函数中的数据做一些事情
  • 天气图像在哪里?我得到以下回应。
  • &lt;time day=​"2016-06-17"&gt; ​&lt;symbol number=​"802" name=​"scattered clouds" var=​"03d"&gt;​&lt;/symbol&gt;​ &lt;precipitation&gt;​&lt;/precipitation&gt; ​&lt;windDirection deg=​"293" code=​"WNW" name=​"West-northwest"&gt;​&lt;/windDirection&gt; ​&lt;windSpeed mps=​"5.01" name=​"Gentle Breeze"&gt;​&lt;/windSpeed&gt;​ &lt;temperature day=​"308.94" min=​"302.56" max=​"308.94" night=​"302.56" eve=​"308.1" morn=​"308.94"&gt;​&lt;/temperature&gt;​ &lt;pressure unit=​"hPa" value=​"955.41"&gt;​&lt;/pressure&gt; ​&lt;humidity value=​"39" unit=​"%"&gt;​&lt;/humidity&gt;​ &lt;clouds value=​"scattered clouds" all=​"36" unit=​"%"&gt;​&lt;/clouds&gt; ​&lt;/time&gt;
  • 除了你在哪里问什么,我什么都不知道。如果您想了解有关 openweather api 的更多信息,请访问它的文档:openweathermap.org/api
猜你喜欢
  • 1970-01-01
  • 2013-09-25
  • 2021-07-11
  • 2017-10-23
  • 1970-01-01
  • 2022-01-21
  • 1970-01-01
  • 1970-01-01
  • 2016-10-21
相关资源
最近更新 更多