如何使用js实施openweathermap api的try and catch?

问题描述

我正在使用openweathermap API进行分配,并且得到的部分反馈是,例如,如果Winderemere(城市)无法正常工作,则执行错误处理,我编写的代码如下(JS / Jquery)。另外,我还是新手,因此非常感谢您提出任何改进建议:

try {
    $.getJSON("https://api.openweathermap.org/data/2.5/weather?q=Windermere,uk&units=metric&APPID=(apinumber)",function (data) {
    
    let icon = "https://openweathermap.org/img/wn/" + data.weather[0].icon + ".png";

    let temp = Math.floor(data.main.temp);
    let weather = data.weather[0].main;
    let temp_max = Math.floor(data.main.temp_max);
    let temp_min = Math.floor(data.main.temp_min);

    $(".icon").attr("src",icon);
    $(".weather").append(weather);
    $(".temperature").append(temp);
    $(".temp_max").append(temp_max);
    $(".temp_min").append(temp_min);
})
} catch (error) {
  alert("Error detected");
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)