不能't从openWeatherMap api JavaScript获取数据

couldn't get the data from the openWeatherMap api JavaScript

本文关键字:api JavaScript 获取 数据 openWeatherMap 不能      更新时间:2023-09-26

不知道问题出在哪里,但所选div中没有显示任何内容。

    $(document).ready(function(){
  var lat, lng, data;
  // get the current location
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function(position) {
          lat = position.coords.latitude;
          lng = position.coords.longitude;
        });
    }else {
      alert('Geo Location feature is not supported in this browser.');
   }

//get the data from the openWeatherMap API
  function getData(){
    $.getJSON('api.openweathermap.org/data/2.5/weather?lat='+lat+'&lon='+lng+'', function(json){
      data = json;
    });    
  }
  $('button').on('click', function(){
    getData();
    $(this).remove();
    $('.container').removeClass('hidden');
    $('#location').appendTo(data.name);
    $('#Weather-condition').appendTo(data.weather.main); 
    $('#wind-speed').appendTo(data.wind);
  });
});

看起来有一个拼写错误:getJESON而不是getJSON!

您可以从两侧中找到此问题

  • 您是否从ajax获取数据?PS:您可以添加c.log(数据)
  • 你在网页上成功显示数据了吗