如何使用meteor进行API调用
好的,这里是 twitter API,
Ok here is the twitter API,
http://search.twitter.com/search.atom?q=perkytweets
谁能给我任何关于如何使用 Meteor
Can any one give me any hint about how to go about calling this API or link using Meteor
更新::
这是我尝试过的代码,但没有显示任何响应
Here is the code that i tried but its not showing any response
if (Meteor.isClient) {
Template.hello.greeting = function () {
return "Welcome to HelloWorld";
};
Template.hello.events({
'click input' : function () {
checkTwitter();
}
});
Meteor.methods({checkTwitter: function () {
this.unblock();
var result = Meteor.http.call("GET", "http://search.twitter.com/search.atom?q=perkytweets");
alert(result.statusCode);
}});
}
if (Meteor.isServer) {
Meteor.startup(function () {
});
}
您正在定义检查Twitter Meteor.method inside 一个客户端范围的块.因为您不能从客户端调用跨域(除非使用 jsonp),所以您必须将此块放在 Meteor.isServer
块中.
You are defining your checkTwitter Meteor.method inside a client-scoped block. Because you cannot call cross domain from the client (unless using jsonp), you have to put this block in a Meteor.isServer
block.
顺便说一句,根据文档,您的 checkTwitter 的客户端 Meteor.method
函数只是服务器端方法的存根.您需要查看文档以获得有关服务器端和客户端 Meteor.methods
如何协同工作的完整说明.
As an aside, per the documentation, the client side Meteor.method
of your checkTwitter function is merely a stub of a server-side method. You'll want to check out the docs for a full explanation of how server-side and client-side Meteor.methods
work together.
以下是 http 调用的工作示例:
Here is a working example of the http call:
if (Meteor.isServer) {
Meteor.methods({
checkTwitter: function () {
this.unblock();
return Meteor.http.call("GET", "http://search.twitter.com/search.json?q=perkytweets");
}
});
}
//invoke the server method
if (Meteor.isClient) {
Meteor.call("checkTwitter", function(error, results) {
console.log(results.content); //results.data should be a JSON object
});
}