44

好的,这里是推特 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 () {
    });
}
4

5 回答 5

56

您正在客户端范围的块中定义您的checkTwitter Meteor.method 。 因为你不能从客户端调用跨域(除非使用jsonp),你必须把这个块放在一个块中。Meteor.isServer

顺便说一句,根据文档,您的 checkTwitter 函数的客户端Meteor.method只是服务器端方法的一个存根。您需要查看文档以获取有关服务器端和客户端如何协同工作的完整说明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
    });
}
于 2013-01-14T17:39:44.677 回答
29

这可能看起来很初级 - 但 HTTP 包默认情况下不会出现在您的 Meteor 项目中,并且要求您按点菜方式安装它。

在命令行上:

  1. 只是流星:
    流星添加http

  2. 陨石:
    mrt加http

Meteor HTTP 文档

于 2013-09-20T12:45:01.897 回答
6

客户端上的 Meteor.http.get 是异步的,所以你需要提供一个回调函数:

Meteor.http.call("GET",url,function(error,result){
     console.log(result.statusCode);
});
于 2013-01-14T16:17:43.183 回答
4

使用Meteor.http.get. 根据文档

Meteor.http.get(url, [options], [asyncCallback]) Anywhere
Send an HTTP GET request. Equivalent to Meteor.http.call("GET", ...).

这些文档实际上包含了一些使用 Twitter 的示例,因此您应该能够开始使用它们。

于 2013-01-14T15:02:35.853 回答
0

在服务器端,如果您提供对 http.get 的回调,它将是异步调用,因此我对客户端未定义返回的解决方案是

var 结果 = HTTP.get(iurl); 返回结果.data.response;

因为我没有将调用传回给 HTTP.get,所以它一直等到我得到响应。希望能帮助到你

于 2016-01-28T21:21:23.817 回答