无法从openWeatherMap api获取数据JavaScript

无法从openWeatherMap api获取数据JavaScript

问题描述:

不知道问题出在哪里,但在选定的div中没有​​显示任何内容。无法从openWeatherMap api获取数据JavaScript

$(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); 
    }); 

}); 
+0

你检查任何错误控制台成功的数据显示? – yuriy636

+0

没有错误@ yuriy636。 – SenDjasni

+0

您是否从您发布的代码以外的地方调用'getData()'?请张贴您的HTML。 – yuriy636

它看起来像你有一个错字:getJESON而不是getJSON!

+0

仍然没有工作。 – SenDjasni

+0

啊,刚发现另一个错字:lng与大写g! – csabinho

+0

谢谢你,但它似乎是问题在其他地方@csabinho – SenDjasni

,你可以从两个方面寻找这个问题

  • 你从阿贾克斯得到的数据? PS:你可以添加c.log(数据)
  • 你在你的网页
+0

之后控制台 数据变量在这种情况下是UNDEFINED,但为什么? – SenDjasni

+0

我认为你的网址有误,请检查'lat'和lnG字段 – Broven