【问题标题】:Using Jquery to display JSON data from API - showing undefined使用 Jquery 显示来自 API 的 JSON 数据 - 显示未定义
【发布时间】:2017-02-19 11:46:49
【问题描述】:

我正在使用 jquery 和 json 数组在网站上显示当前的月相和日出/设置时间。这适用于日出/设置,但对于月相我遇到了问题。

当“closestphase”在请求数据的同一天时,其他 2 个字段被删除 - “currentphase”和“fracillum”。这意味着我不能每月显示 4 次相位或电流照明...

例如,下面的链接显示了 fracillum 和当前阶段: http://api.usno.navy.mil/rstt/oneday?date=today&coords=54.97N,1.61W&tz=0

但在此数据中被省略: http://api.usno.navy.mil/rstt/oneday?date=01/28/2017&coords=54.97N,1.61W&tz=0

我已经编写了下面的代码来说明如果 curphase 和 fracillum 未定义 并且 最接近的相位是“新月”或“满月”,则显示 fracillum 为“0% 或“100%”分别。

请有人能指出我做错了什么吗?

$(document).ready(function(){
  $.get("http://api.usno.navy.mil/rstt/oneday?date=01/28/2017&coords=54.97N,1.61W&tz=0", function(data){
    checkPhase(data);
  });
});
$(document).ready(function(){
  $("#suntimes").click(function(){     
    $("#sun").toggle("slide");
  });
});
$(document).ready(function(){
  $("#moontimes").click(function(){     
    $("#moon").toggle("slide");
  });
});
$(document).ready(function(){
  $("#Mobilesuntimes").click(function(){     
    $("#Mobilesun").slideToggle();
  });
});
$(document).ready(function(){
  $("#Mobilemoontimes").click(function(){     
    $("#Mobilemoon").slideToggle();
  });
});

function checkPhase(data){
  if(data.curphase == "undefined" && data.closestphase.phase == "New Moon"){data.fracillum == "0%";}
  else if(data.curphase == "undefined" && data.closestphase.phase == "Full Moon")
  {data.fracillum == "100%";}

  $("#sun").append("<p><b>Newcastle, UK</b><br />Sunrise: " + data.sundata[1].time + " AM<br />Sunset: " + data.sundata[3].time + " PM</p>" );
  $("#moon").append("<p><b>Newcastle, UK</b><br />Percentage Illuminated: " + data.fracillum + "<br />Moon Phase: " + data.curphase + "</p>" );
  $("#Mobilesun").append("<p><b>Newcastle, UK</b><br />Sunrise: " + data.sundata[1].time + " AM<br />Sunset: " + data.sundata[3].time + " PM</p>" );
  $("#Mobilemoon").append("<p><b>Newcastle, UK</b><br />Percentage Illuminated: " + data.fracillum + "<br />Moon Phase: " + data.curphase + "</p>"
  );
}

【问题讨论】:

    标签: javascript jquery html json api


    【解决方案1】:

    使用typeof data.curphase == "undefined" 而不是data.curphase == "undefined"

    var a = 1;
    
    if ( typeof a == "undefined" )
      alert (" a is undefined" )
    
    if ( typeof b == "undefined" ) 
      alert (" b is Undefined" )

    【讨论】:

    • typeof 听起来像是检查undefined 的更好解决方案,因为它更稳定(http://stackoverflow.com/a/3390426/1751277)。我没想到。
    • 感谢您的回答,我实际上首先看到了下面的答案并使用了它 - 它似乎有效,但再次感谢
    • yes typeof 更可靠并防止错误。 @floriangosse。欢迎您@JRat-UK
    • 尝试结合@Eisa和我的解决方案以获得最稳定的检查。
    • 我试试看!一旦我的网站上线,我会在这里发表评论,这样你就可以看到你帮助我做了什么:)
    【解决方案2】:

    如果你想检查undefined 值,你应该使用value === undefined 而不是value === "undefined",因为你检查curphase 是否是字符串"undefined"。

    此外,您没有为 data.fracillum 赋值,因为比较 (data.fracillum == "0%";) 而不是赋值 (data.fracillum = "0%";)。

    你的情况应该是这样的:

    if (data.curphase === undefined && data.closestphase.phase === "New Moon") {
      data.fracillum = "0%";
    } else if (data.curphase === undefined && data.closestphase.phase === "Full Moon") {
      data.fracillum = "100%";
    }
    

    【讨论】:

    • 我用过这个,它似乎工作得很好,谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-23
    • 2015-09-11
    • 1970-01-01
    • 1970-01-01
    • 2013-02-25
    • 1970-01-01
    相关资源
    最近更新 更多