【发布时间】:2015-03-18 13:43:50
【问题描述】:
我正在创建这个简单的天气应用程序,该应用程序从 openweathermap.org 获取天气数据。从这里我创建这样的 url(你在哪里获取 xml 数据):
http://api.openweathermap.org/data/2.5/forecast/weather?q=Koper,slovenia&mode=xml&units=metric
所以对于我的应用,我使用 jquery 和这段代码从 api 获取数据:
$.ajax({
type: 'GET',
dataType: 'xml',
url: "http://api.openweathermap.org/data...",
}).done(function(data){
xmlDoc = data;
//from here I work with xml that I get, and put data into array to display chart
var time=xmlDoc.getElementsByTagName('time');
...
}).fail(function(){
location.reload(); //this is not a good solution
});
这很好,但我的问题是下一个:有时来自 openweathermap 的服务器不响应我的“url”调用并收到消息错误 404。然后如果你刷新页面(或 url)几个次,然后它的工作原理。对于临时解决方案,我添加了“失败”功能以重新加载页面(location.reload)。但这不是一个好的解决方案,因为当服务器调用失败时,页面会不断重新加载。
知道如何解决这个问题,当服务器无法获取数据时,它会尝试再次重新连接... tnx 寻求帮助
【问题讨论】: