$.getJSON 没有 return 数据

$.getJSON doesn't return data

我正在参加有关 freecodecamp 的课程,目前我被困在天气应用滑索中。我呼叫的 API 是 OpenWeatherMap。我的问题是 $.getJson 没有返回数据,即使 link 是正确的。我在 $.getJSON 之外放置了警报,它工作正常。我将分享源代码:

所有这些都是在codepen上完成的如果你没有看到html标签是因为codepen自己做的。 CSS 也是这样做的。

HTML

<head> 
  <title>Weather App</title>
  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
</head>
<body>
  <h1>Free Code Camp Zipline</h1>
  <h2>Local Weather App!</h2>
  <p id="latitude"></p>
  <p id="longitude"></p>
  <div id="weather"></div>
  <footer>
    <p>Copyright © Luis M. Alvarez 2016. All Rights Reserved</p>
  </footer> 
</body>

CSS

     body {
        margin: 0;
        font-family: "Georgia";
     }

    h1, h2 {
       text-align: center;
    }
    p {
     font-size: 20px;
     text-align: center;
    }

Javascript

    $(document).ready(function(){
    ///Geolocation

    //Find the geolocation
    if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
       $("#latitude").html("latitude: " + position.coords.latitude);
       $("#longitude").html("longitude: " + position.coords.longitude);  

       ///Weather API 

       //Setup for weather app    
       var key = "d160d975b9920be65fcf14313e95afb4";

       var weatherNow = "http://api.openweathermap.org/data/2.5/weather?lat=" + position.coords.latitude + "&lon=" + position.coords.longitude + "&APPID=" + key + "&units=metric";

      //Get the weather
         //Here alert does work
      $.getJSON(weatherNow, function(data) { //Where weatherNow is on this line the alert works too example: $.getJSON(alert(weatherNow), function(data) {
         //Here the alert doesn't work
         alert(weatherNow);

     });

   });
 }; 
});

注释已添加到此处,以免它们弄乱代码。请详细解释为什么它不起作用以及如何使其起作用。

项目链接: https://codepen.io/Zero720/pen/RoOwaw

http://openweathermap.org/current

如果您熟悉浏览器中的 JavaScript 控制台,

编程 JavaScript 将容易得多。这是将出现在您的错误中的错误:

Mixed Content: The page at 'https://codepen.io/Zero720/pen/RoOwaw' was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint 'http://api.openweathermap.org/data/2.5/weather?lat=42.9976979&lon=-77.50486959999999&APPID=d160d975b9920be65fcf14313e95afb4&units=metric'. This request has been blocked; the content must be served over HTTPS.

.