gpt4 book ai didi

javascript - 使用 jQuery 从 API 显示天气

转载 作者:行者123 更新时间:2023-12-03 02:19:53 25 4
gpt4 key购买 nike

我正在尝试使用weatherunderground api来显示天气信息。

运行我的代码时,我收到以下消息。

TypeError: undefined is not an object (evaluating 'parsed_json['location']['city']')

我不确定这是为什么或者如何解决这个问题。我是 JavaScript 和 jQuery 新手。

这是我的代码

$(document).ready(function() {
$.ajax({
url : "http://api.wunderground.com/api/dddb7758c5d626fc/geolookup/conditions/q/IA/Cedar_Rapids.json",
dataType : "jsonp",
success : function(parsed_json) {
var location = parsed_json['location']['city'];
var temp_f = parsed_json['current_observation']['temp_f'];
alert("Current temperature in " + location + " is: " + temp_f);
}
});
});

更新我的 api 后它可以工作,但我仍然在控制台中收到相同的消息

最佳答案

您的成功函数意味着您已到达您的 API,但此 API 可能会向您返回错误!

当我访问该网址时,看看我得到了什么:

{
"response": {
"version":"0.1",
"termsofService":"http://www.wunderground.com/weather/api/d/terms.html",
"features": {
}
,
"error": {
"type": "invalidkey"
,"description": "this key is not valid due to exceeding rate plan"
}
}
}

因此,请检查您的 API key ,并在执行任何操作之前检查是否存在错误。同样,始终检查所有内容,因此如果没有错误,请在尝试对其执行其他操作之前检查值是否存在!

关于javascript - 使用 jQuery 从 API 显示天气,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49210422/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com