javascript - 为什么 console.log 在这种情况下不起作用?开放天气 map API

标签 javascript jquery json openweathermap

我正在开发一个天气应用程序项目,无法理解为什么第 20 行的 console.log 在下面不起作用:

$(document).ready(function() {
  var long;
  var lat;
  var temp;
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
      long = position.coords.longitude;
      lat = position.coords.latitude;

      var api =
        "api.openweathermap.org/data/2.5/weather?lat=" +
        lat +
        "&lon=" +
        long +
        "&appid=xxxxxxxxxxxxxxxxxxxxxxx";
      //console.log(api); 
      $("#data").html("latitude: " + lat + "<br>longitude: " + long);
      $.getJSON(api, function(json) {
        var kelvin = data.main.temp;
        console.log(kelvin);
        
       
      });
    });
  }
});
body{
  height:100vh;
  position:relative;
}
.container{
  margin:auto;
  text-align:center;
  padding:20px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
  <div class="container">
    <h1>Weather App</h1>
    <div id="data">location</div>
    <div id="temperature">Temperature
      <span id="degrees" onclick="">DEGREES</span></div>
    <div class="weather">Weather icon</div>
    
  </div>
  
</body>

我想从 openweathermap api 获取温度,并且我正在测试 kelvin 变量,但它没有显示任何内容。

提前非常感谢,

最佳答案

这不是调用 $.getJSON 的方式。还差得很远。

第一个参数是 URL,您提供的字符串不是 URL,因为 URL 以协议(protocol)开头,例如“https://”。

第二个参数是数据,而不是函数,这是您提供的。

第三个​​参数是成功回调的位置。

首先进行打印输出,以防取消引用失败。

关于javascript - 为什么 console.log 在这种情况下不起作用?开放天气 map API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45037620/

相关文章:

javascript - 使用 T 的参数类型 U 获取类型 T 函数的参数类型

python - 查找匹配另一个字典的字典

java - JSONArray 到字符串数组

javascript - 获取1课内2课的内容

javascript - 从 localStorage 获取值并将其 append 到类

jquery - 在实时函数中查找元素的索引

json - 如何向用户显示 json feed?

javascript - 来自资源的 Angular ng-repeat 复选框并设置选中

javascript - EventSource/SSE(服务器发送事件)- 安全

javascript - 2 个具有相同类名的 css 文件,具有该类名的元素具有两种样式