jQuery.Deferred异常:无法读取未定义的属性'0'“TypeError:无法读取未定义的属性'0'

时间:2017-11-05 18:48:36

标签: jquery api

我继续使用codepen:jQuery.Deferred exception: Cannot read property '0' of undefined" "TypeError: Cannot read property '0' of undefined在控制台上收到此消息。

我的javascript代码:

var api_key = "fa15d403d0ab55e3cfd6e0867bbb0114";
$(document).ready(function(){
  var loc;
  //Call the location   
$.getJSON('https://ipinfo.io', function(data){
  loc = data.loc.split(",");
  console.log(loc);
 });
  $.getJSON('http://api.openweathermap.org/data/2.5/weather?lat=' + loc[0]  +  '&lon=' + loc[1] + '&APPID='+ api_key, function(weather){
         console.table("c ,",weather);
          });
});

1 个答案:

答案 0 :(得分:1)

这意味着您正在尝试阅读<!DOCTYPE html> <html> <head> <title>Comments</title> </head> <body> <form id="commentForm" name="commentForm" onsubmit="return(validate());"> <div class="row"> <div class="col-sm-12 form-group"> <textarea class="form-control" id="comments" name="comments" placeholder="Leave comment..." rows="3"></textarea><br> <button class="btn pull-right" type="submit">Send</button> </div> </div> </form> </body> </html>对象的0属性。

根据您分享的代码,此处发生的代码:undefined - loc[0]未定义,因此会抛出错误。

它未定义的原因是异步排序 - 你的第一个功能还没有设置回调,而是设置loc。尝试将第二个电话放在第一个电话中(在loc电话之后)而不是之后。

相关问题