Javascript node.js:Yahoo weather的jQuery插件';打印数据
我让node.js使用jQuery和这个插件:。现在我想将天气数据用于其他服务,而不是将其放入HTML中,但我无法访问/打印数据。error函数起作用,success函数不起作用Javascript node.js:Yahoo weather的jQuery插件';打印数据,javascript,jquery,node.js,yahoo,weather,Javascript,Jquery,Node.js,Yahoo,Weather,我让node.js使用jQuery和这个插件:。现在我想将天气数据用于其他服务,而不是将其放入HTML中,但我无法访问/打印数据。error函数起作用,success函数不起作用 var jsdom = require("jsdom"); jsdom.env({ html: '<html><body><div id="weather"></div></body></html>', scripts: [ 'ht
var jsdom = require("jsdom");
jsdom.env({
html: '<html><body><div id="weather"></div></body></html>',
scripts: [
'http://code.jquery.com/jquery-2.1.1.min.js',
'http://cdnjs.cloudflare.com/ajax/libs/jquery.simpleWeather/3.0.2/jquery.simpleWeather.min.js'
],
done: function(errors, window) {
var $ = window.jQuery;
$.simpleWeather({
location: 'Paris',
woeid: '615702',
unit: 'c',
success: function(weather) {
console.log(weather.temp+'°'+weather.units.temp);
},
error: function(error) {
console.log(error.message);
}
});
}
});
var jsdom=require(“jsdom”);
jsdom.env({
html:“”,
脚本:[
'http://code.jquery.com/jquery-2.1.1.min.js',
'http://cdnjs.cloudflare.com/ajax/libs/jquery.simpleWeather/3.0.2/jquery.simpleWeather.min.js'
],
完成:函数(错误,窗口){
var$=window.jQuery;
$.SimpleWither({
地点:'巴黎',
woeid:'615702',
单位:'c',
成功:功能(天气){
控制台日志(天气温度+'°'+天气单位温度);
},
错误:函数(错误){
console.log(错误消息);
}
});
}
});
这就是它通常的样子
插件也有一个节点模块,但我不知道如何让它工作,也没有文档。jsdom没有实现XMLHttpRequest。有关可能的解决方案,请参阅问题。我建议不要使用jQuery插件,而是直接使用参考底图数据 SimpleMather使用yahoo API检索天气信息 为了以易于使用的JSON格式获取天气信息,您可以使用yahoo的查询语言 1。获取位置ID 例如,如果我想获得“瑞典斯德哥尔摩”的天气信息,我可以查询
SELECT woeid FROM geo.places where text="Stockholm, Sweden" LIMIT 1
将返回“woeid”:“906057”
2。获取天气信息
现在是使用我们在上一步中检索到的位置ID的时候了
SELECT item.condition.temp FROM weather.forecast WHERE woeid = 906057
这将返回“temp”:“81”
自动化it
为了通过编程方式接收节点中的温度,我建议使用该模块。您可以从查询生成器页面复制API内点。在这个例子中是
我想您可以很容易地从这里开始,但为了完整起见,我将提供一个简单的节点示例:
var request = require('request');
var url = 'https://query.yahooapis.com/v1/public/yql?q=SELECT%20item.condition.temp%20FROM%20weather.forecast%20WHERE%20woeid%20%3D%20906057&format=json';
request(url, function (err, resp, body) {
if (err || resp.statusCode != 200)
return console.log('Could not get weather information');
var json = JSON.parse(body);
console.log('Temperature in Stockholm:', json.query.results.channel.item.condition.temp);
});
那太完美了。非常感谢!我不知道Yahoo API能做到这一点。没问题,很高兴我能帮上忙。顺便说一下,我相信有一些YQL节点模块,因此您可以进一步简化示例,但我不熟悉它,所以我将让您测试它