带有JSON获取请求的未捕获的TypeError

时间:2017-09-01 04:28:10

标签: javascript jquery json typeerror uncaught-typeerror

main是我希望从天气API获取的JSON对象属性,但我收到以下错误:

  

未捕获的TypeError:无法读取属性' main'未定义的

出了什么问题?

HTML code:

<!-- display current temperature in Fahrenheight (default) -->
<div id="temp"></div>
<!-- display user's curent location -->
<div id="pos"></div>
<button id="loc">Show my location</button>
<button id="switch">Switch Metric Unit</button>

// return temperature and weather conditions for user's current location
var temp = document.getElementById('temp');
temp.innerHTML = "";
var tempStr = document.getElementById('temp').innerHTML;
var latitude;
var longitude;

JS代码:

if (navigator.geolocation) {
  navigator.geolocation.getCurrentPosition(function(position) {
    latitude = position.coords.latitude;
    longitude = position.coords.longitude;
    // pos = position.coords.latitude + ", " + position.coords.longitude;
  })};

  $.getJSON("https://fcc-weather-api.glitch.me/api/current?lat=" + latitude + "&lon=" + longitude, 
    setTimeout(function(data) {
      $('#temp').html(celsiusToFahrenheit(data.main.temp) + "&deg;F");
    }), 1000)

0 个答案:

没有答案