[英]How to make an API call using meteor
好的,這是twitter API,
http://search.twitter.com/search.atom?q=perkytweets
任何人都可以給我任何關於如何使用Meteor調用此API或鏈接的提示
更新::
這是我嘗試的代碼,但它沒有顯示任何響應
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 () {
});
}
您要定義checkTwitter Meteor.method客戶范圍的塊內 。 因為您無法從客戶端調用跨域(除非使用jsonp),您必須將此塊放在Meteor.isServer
塊中。
Meteor.method
一下,根據文檔 ,checkTwitter函數的客戶端Meteor.method
只是服務器端方法的存根 。 您需要查看文檔,以獲得有關服務器端和客戶端Meteor.methods
如何Meteor.methods
工作的完整說明。
這是http調用的一個工作示例:
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
});
}
這可能看起來很簡陋 - 但默認情況下,您的Meteor項目中不會出現HTTP程序包,並要求您單獨安裝它。
在命令行上:
只是流星:
流星添加http
隕石:
mrt添加http
客戶端上的Meteor.http.get是異步的,因此您需要提供回調函數:
Meteor.http.call("GET",url,function(error,result){
console.log(result.statusCode);
});
使用Meteor.http.get
。 根據文檔 :
Meteor.http.get(url, [options], [asyncCallback]) Anywhere Send an HTTP GET request. Equivalent to Meteor.http.call("GET", ...).
文檔實際上包含一些使用Twitter的示例,因此您應該能夠開始使用它們。
在服務器端,如果你提供回調到http.get,它將是異步調用,所以我在客戶端的未定義返回的解決方案是
var result = HTTP.get(iurl); return result.data.response;
因為我沒有將呼叫傳回HTTP.get,所以它一直等到我得到回應。 希望能幫助到你
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.