如何使用meteor进行API调用 [英] How to make an API call using meteor

查看:26
本文介绍了如何使用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
    });
}

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

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