我正在为Firefox构建一个javascript组件,它将采用邮政编码并返回当前的天气状况。javascript xpcom组件下载天气地下天气数据
The sample code that weather underground uses jQuery,但据我所知,我不能在JavaScript组件中包含此代码,因为JavaScript没有包含其他JavaScript文件的功能。
无论如何,我已经构建了我的骨架代码。这需要在邮政编码并建立链接
(例如:http://api.wunderground.com/api/e17115d7e24a448e/geolookup/conditions/q/22203.json)
我试图下载来自该网址的数据,通过下面的方法:
getWeatherByUrl: function(url)
{
var persist = Components.classes["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"].createInstance(Components.interfaces.nsIWebBrowserPersist);
var file = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties).get("ProfD",Components.interfaces.nsILocalFile);
file.append("weather-forecaster.dat");
var urlURI = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService).newURI(url, null, null);
persist.saveURI(urlURI,null,null,null,"",file);
return url;
}
这应该下载文件到用户的配置文件目录。确实确实在那里创建文件。但是,它看起来并不像它包含来自地下天气的json数据。
究竟发生了什么?我将如何下载文件?我相信有一个查询会在该URL传递给地下天气时进行,但这应该不重要,因为.json页面是从它们中吐出来的,对吧?
有没有办法做到这一点,而无需下载文件,但通过流媒体和解析它?