【发布时间】:2016-06-13 19:36:18
【问题描述】:
我正在https://www.freecodecamp.com/challenges/show-the-local-weather做这个项目
如果您需要,我的完整项目在这里:https://codepen.io/tugrulz/pen/beEmJb?editors=0010
function fetchWeather() {
var ap = "http://api.openweathermap.org/data/2.5/weather?";
var key = "&appid=061f24cf3cde2f60644a8240302983f2";
var lat = "35";
var lon = "139";
var api = ap + "lat=" + lat + "&lon=" + lon + key;
$(".location").html(api);
$.getJSON(api, function(data) {
alert("sa");
$(".location").html("oldu mu?");
})
.done(function() {
alert( "second success" );
})
.fail(function(error) {
alert( error );
})
.always(function() {
alert( "complete" );
});
}
打印:http://api.openweathermap.org/data/2.5/weather?lat=35&lon=139&appid=061f24cf3cde2f60644a8240302983f2
问题是,我打印的链接在 chrome 中正确工作,但 getJSON 不起作用。它的失败功能有效。
我该怎么办? 知道如何打印错误消息吗?打印(错误);没有帮助。既不是涉及 getJson 的 try and catch 语句。
【问题讨论】:
-
你确定你打电话给
fetchWeather()。看看这个CodePen。它基本上是您的代码,我将响应名称写入 div。所以它起作用了。 -
是的,我这样做了,但它处于就绪状态,我使用引导程序,但当我更改它们时,这些都没有改变。有趣的。我会用你的作为模板,但我仍然很好奇项目之间有什么区别。
-
我真的不知道我们的两个版本有什么区别。我更新了我的笔,将
fetchWeather()调用包含在$(document).ready()中,它仍然有效。
标签: javascript json frontend