未定义的响应来自侧边栏小工具中 XHR 的文本
Undefined responseText from XHR in sidebar gadget
我正在尝试编写一个小工具,显示 api.openweathermap.org 的当前温度。该小工具通过 setTimeout "循环"中的异步 XMLHttpRequest 提取信息(因此天气数据每隔几分钟更新一次(,但有时 XHR 的响应文本是未定义的,这当然会引发错误并停止循环,并不可避免地停止更新过程。
Fiddler 显示流量熄灭,响应返回有效且预期的数据,但 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,对我来说没有花哨的库。
请指教。
更新
我做了一些更改和测试,这是我发现的:
我使用了不同的网络服务 - 雅虎天气和小工具完美运行。Yahoo返回XML,OpenWeatherApi - JSON。我还不知道这是否与我的问题有关,因为正如我之前所说 - OWA 有时会起作用。
CORS绝对不是小工具的问题。这个小提琴无法检索数据,在检查器中我可以看到一条消息,指出
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.
然而,在我的小工具中,检索数据没有问题。
这很可能是由于浏览器中实现的跨域安全策略(跨域资源共享 – CORS、W3C(机制。通过javascript对来自不同域的资源的HTTP请求将失败(如果服务器支持,您可以使用JSONP解决此问题(。要启用 CORS,您尝试连接的服务器需要将您的域添加到"访问控制允许来源"HTTP 标头或允许所有域(使用星号通配符"*"表示(。
MDN 文章供参考 - https://developer.mozilla.org/en/docs/HTTP/Access_control_CORS
有人已经记录了这个问题:http://bugs.openweathermap.org/issues/152
- 用于高级文本的格式化工具
- 使用CSS/JavaScript更改剑道图工具提示文本颜色
- Highcharts工具提示不显示corrosconding文本值,而是显示'未定义'
- 如何在Google Geochart图表的工具提示文本中添加新行
- ExtJ将工具提示添加到网格单元格文本中
- 将文本工具添加到绘制应用程序HTML5画布
- 文本编辑工具栏解决方案 (js),具有低级自定义
- JQuery文本编辑器-在工具栏中添加其他下拉列表
- 数组中的工具提示/悬停文本
- 使用 jQuery 设置工具提示文本
- 如果文本在高图表中较多,则工具提示无法正常工作
- 在当前工具提示文本 JQuery 旁边追加或添加新文本
- 丰富的面孔 :如何在输入数字滑块工具提示的值旁边添加静态文本
- 如何将文本框弹出窗口(Jquery 工具提示或类似工具)添加到画布中的 Fabric JS 图像
- 在 Ajax 工具包 htmleditorextender 中设置按钮单击事件的文本
- free-jqgrid:保存、加载和应用过滤器数据的更简单方法,包括过滤器工具栏文本和页面设置
- 未定义的响应来自侧边栏小工具中 XHR 的文本
- 添加文本工具,在Html5画布上拖放并调整其大小
- 是否可以创建当鼠标悬停在
标记内的文本上时出现的工具提示
- 在页面加载时自动对焦 dx文本框 并将 dx工具提示显示为验证消息