如何使流星使用API​​调用 [英] How to make an API call using meteor

查看:369
本文介绍了如何使流星使用API​​调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

确定这里是Twitter的API,

  http://search.twitter.com/search.atom?q=perkytweets

任何一个可以给我如何去调用这个API或链接使用任何暗示流星

更新:

下面是code,我试过,但它没有显示任何回应

 如果(Meteor.isClient){
    Template.hello.greeting =功能(){
        返回欢迎来到HelloWorld的;
    };    Template.hello.events({
        点击输入':功能(){
            checkTwitter();
        }
    });    Meteor.methods({checkTwitter:功能(){
        this.unblock();
        VAR的结果= Meteor.http.call(GET,http://search.twitter.com/search.atom?q=perkytweets);
        警报(result.status code);
    }});
}如果(Meteor.isServer){
    Meteor.startup(函数(){
    });
}


解决方案

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

顺便说一句,每个文档,客户端 Meteor.method 仅仅是一个服务器端方法的存根。你想查看的文档的服务器端和客户端 Meteor.methods 如何协同工作的完整说明。

下面是HTTP调用的工作例如:

 如果(Meteor.isServer){
    Meteor.methods({
        checkTwitter:功能(){
            this.unblock();
            返回Meteor.http.call(GET,http://search.twitter.com/search.json?q=perkytweets);
        }
    });
}//调用服务器方法
如果(Meteor.isClient){
    Meteor.call(checkTwitter功能(错误,结果){
        的console.log(results.content); //results.data应该是一个JSON对象
    });
}

Ok here is the twitter API,

http://search.twitter.com/search.atom?q=perkytweets

Can any one give me any hint about how to go about calling this API or link using Meteor

Update::

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 () {
    });
}

解决方案

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.

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.

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
    });
}

这篇关于如何使流星使用API​​调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆