我正在尝试编写一个小工具,用于显示api.openweathermap.org当前的温度。 小工具通过setTimeout“循环”中的异步XMLHttpRequest获取信息(因此天气数据每隔几分钟更新一次),但有时XHR的responseText未定义,这当然会引发错误并停止循环,并且不可避免地会导致更新过程。来自侧边栏小工具中XHR的未定义responseText
提琴手显示流量熄灭,响应返回有效和预期的数据,但XHR无法以某种方式访问它。
相关的JS位:
var timer; // these three
var locStr; // are defined
var weather; // elsewhere
function showWeather()
{
System.Debug.outputString('showWeather()');
if (timer) clearTimeout(timer);
locStr = System.Gadget.Settings.readString("location");
// this is for first-run use only - when the setting is not set, display an appropriate notification to the user
if (locStr == "")
{
cell.style.display = 'none';
welcome.style.display = 'block';
}
else
{
cell.style.display = 'block';
welcome.style.display = 'none';
updateWeather();
}
}
function updateWeather()
{
System.Debug.outputString('updateWeather()');
try
{
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function()
{
System.Debug.outputString('updateWeather()>onreadystatechange ' + xhr.status + ' ' + xhr.readyState);
if (xhr.status === 200)
{
if (xhr.readyState === 4)
{
// this is what makes it fail:
System.Debug.outputString(xhr.responseText);
weather = eval('(' + xhr.responseText + ')'); // i DO get that eval is evil, but works for my purposes well.
// temp, city, wind and clouds are html entities id values
temp.innerHTML = Math.round(weather.main.temp) + '<span>°C</span>';
city.innerHTML = weather.name;
wind.innerHTML = weather.wind.speed + ' m/s';
clouds.innerHTML = weather.clouds.all + '%';
// clears and hides error, if occured in previous fetch
error_title.innerHTML = '';
error_description.innerHTML = '';
error.style.display = 'none';
timer = setTimeout(updateWeather, 1000 * 5);
}
else
{
temp.innerText += '.';
}
}
else
{
error_title.innerText = 'Connectivity error';
error_description.innerText = xhr.status + '.'; // that's really informative...
error.style.display = 'block';
timer = setTimeout(updateWeather, 5000)
}
};
xhr.ontimeout = function()
{
error_title.innerText = 'Connectivity error';
error_description.innerText = 'timed out';
error.style.display = 'block';
timer = setTimeout(updateWeather, 5000);
};
xhr.open('get', 'http://api.openweathermap.org/data/2.5/weather?q=' + locStr + '&units=metric&dummy=' + Math.ceil(100*Math.random()), false);
xhr.setRequestHeader('Cache-Control', 'no-cache');
xhr.send();
}
catch (e)
{
error_title.innerText = e.name;
error_description.innerText = e.message;
error.style.display = 'block';
timer = setTimeout(updateWeather, 5000);
}
}
showWeather()
在窗口的onload事件,然后检查用户是否有偏好设置的位置(市),并开始在updateWeather环路()被调用。
updateWeather()
然后发出请求并正确处理数据,这是可行的,但只要我不尝试访问responseText。
我在我的智慧结束这个问题,但我觉得我在这里错过了一些基本的东西,并且我对JS还不是很有经验。
我想在这里使用纯javascript,对我来说没有花哨的图书馆。
请指教。
更新
我做了一些改变和测试,这里是我发现了什么:
我已经使用了不同的Web服务 - 雅虎天气和小工具完美无缺地工作。雅虎返回XML,OpenWeatherApi - JSON。我还不知道它是否与我的问题有关,因为正如我之前所说 - OWA有时可以工作。
CORS绝对不是小工具的问题。 This小提琴检索不到数据,并在检查我可以看到一条消息,指出
XMLHttpRequest cannot load http://weather.yahooapis.com/forecastrss?w=526363&u=c. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://fiddle.jshell.net' is therefore not allowed access.
然而,在我的小工具,数据是没有问题的检索。
我相信同源策略不是在小工具的环境执行。为了支持我的说法 - 小工具首先起作用,对于前三个“updateWeather()”调用,可以访问“responseText”字段。然后它不是,这是什么让我挠了脑袋。 – Konrad
嗯好吧,听起来和浏览器中的CORS发生的问题完全一样,在检查网络流量时没有错误,并且响应正确,但javascript无法访问它。我不熟悉构建Windows小工具,但我认为他们在某种无铬浏览器中运行?我想检查Fiddler中的标题信息可能会有所帮助。 – heardy