在Raspberry Pi Coder中为openweather发出JSON resquest

时间:2016-05-03 21:30:43

标签: json raspberry-pi openweathermap

我正在使用Raspberry pi上的Coder学习HTML,CSS和Javascript。目前,我正在尝试制作一个显示时间,日期和当前天气的简单页面。 $.getJSON函数中的getWeather()调用出现问题。

键入传递给$.getJSON的URL正常工作(即,页面加载了JSON中的所有信息),但“Got Weather”字符串永远不会显示。我也尝试过使用请求JSON或JSONP数据类型的AJAX调用。这些方法都没有奏效。我错过了什么?

$(document).ready( function() {

    //This code will run after your page loads
    function displayTime() {
        var current_time = new Date();
        var hours = current_time.getHours();
        var minutes = current_time.getMinutes();
        var seconds = current_time.getSeconds();
        var meridiem = "AM"; // default is AM

        var day = current_time.getDay();

        if(seconds < 10) {
            seconds = "0" + seconds;   
        }

        if(minutes < 10) {
            minutes = "0" + minutes;   
        }

        // Set the meridiem for a 12hr clock
        if(hours > 12) {
            hours -= 12; 
            meridiem = "PM"
        } else {
            meridiem = "AM"   
        }
        var clock_div = document.getElementById('clock');
        clock_div.innerText = hours + ":" + minutes + ":" + seconds + " " + meridiem;

        // Depending on the value of 'day', set the corresponding string
        var day_div = document.getElementById('day');

        var weekday = new Array(7);
        weekday[0] = "Sunday";
        weekday[1] = "Monday";
        weekday[2] = "Tuesday";
        weekday[3] = "Wednesday";
        weekday[4] = "Thursday";
        weekday[5] = "Friday";
        weekday[6] = "Saturday";

        var today = weekday[current_time.getDay()];
        day_div.innerText = today;

        // Get the date information
        var date = current_time.getDate();
        // Get the year
        var year = current_time.getFullYear();
        // Get the month and set the string
        var month = new Array(12);
        month[0] = "January";
        month[1] = "February";
        month[2] = "March";
        month[3] = "April";
        month[4] = "May";
        month[5] = "June"; 
        month[6] = "July";
        month[7] = "August";
        month[8] = "September";
        month[9] = "October";
        month[10] = "November";
        month[11] = "December";

        var this_month = month[current_time.getMonth()];

        // set the string 
        var date_div = document.getElementById('date');
        date_div.innerText = this_month + " " + date + " " + year;
    }

    function getWeather() {
        var api_key = REMOVED; // API key for open weather
        var weather_api = "http://api.openweathermap.org/data/2.5/weather?lat=40.115&lon=-88.27&units=imperial&appid=" + api_key;


        var weather_div = document.getElementById('weather');

        $.getJSON(weather_api).then(function(result){
        //alert("City: "+result.city.name);
        //alert("Weather: "+ result.list[0].weather[0].description);
        weather_div.innerText = "Got Weather";
        });

        //weather_div.innerText = "Got Weather";
    }

    // This runs the displayTime function the first time
    displayTime();
    getWeather();

    // This makes the clock "tick" repeatedly by calling it every 1000 ms 
    setInterval(displayTime, 1000);
    setInterval(getWeather, 2000);

});

HTML:

<!DOCTYPE html>
<html>
<head> 
    <title>Coder</title>
    <meta charset="utf-8">
    <!-- Standard Coder Includes -->
    <script>
        var appname = "{{app_name}}"; //app name (id) of this app
        var appurl = "{{&app_url}}";
        var staticurl = "{{&static_url}}"; //base path to your static files /static/apps/yourapp
    </script>
    <link href="/static/apps/coderlib/css/index.css" media="screen" rel="stylesheet" type="text/css"/>
    <script src="/static/common/js/jquery.min.js"></script>
    <script src="/static/common/ace-min/ace.js" type="text/javascript" charset="utf-8"></script>
    <script src="/static/apps/coderlib/js/index.js"></script>
    <script>
        Coder.addBasicNav();
    </script>
    <!-- extra inludes to get weather from OpenWeather -->
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <script>window.jQuery || document.write('<script src="js/vendor/jquery-1.11.0.min.js"><\/script>')</script>
    <script src="js/vendor/jquery-ui.min.js"></script>
    <!-- End Coder Includes -->

    <!-- This app's includes -->
    <link href="{{&static_url}}/css/index.css" media="screen" rel="stylesheet" type="text/css"/>
    <script src="{{&static_url}}/js/index.js"></script>
    <!-- End apps includes -->
</head>
<body>
    <div id='day'></div><br>
    <div id='date'></div>
    <div id='clock'></div>
    <div id='weather'></div>
</body>
</html>

CSS:

body {
 background-color: black;   
}

#day {
    height:100px;
    width: 300px;
    margin-left: 10px;
    padding-top: 50px;
    position: fixed;
    top: 0px; left: 20px;
    font-family: courier, monospace;
    text-align: center;
    color: white;
    font-size: 30px;
    font-weight: bold;
}

#date {
    height:100px;
    width: 300px;
    margin-left: 10px;
    padding-top: 50px;
    position: fixed;
    top: 50px; left: 20px;
    font-family: courier, monospace;
    text-align: center;
    color: white;
    font-size: 20px;
}

#clock {
    height:100px;
    width: 300px;
    margin-left: 10px;
    padding-top: 50px;
    position: fixed;
    top: 100px; left: 20px;
    font-family: courier, monospace;
    text-align: center;
    color: white;
    font-size: 20px;
}

1 个答案:

答案 0 :(得分:0)

我发现了这个问题。打开开发人员控制台显示“阻止加载混合活动内容......”的错误。我似乎需要更好地熟悉开发人员工具。

在Coder中构建的应用程序可通过HTTPS访问。但是,对openweather的调用是通过HTTP进行的。只有拥有专业(付费)订阅时,openweather API才允许HTTPS呼叫。幸运的是,https://forecast.io每天允许一定数量的免费通话并使用HTTPS。