javascript - 如何使用 meteor 进行 API 调用

好的,这里是 twitter API,

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

谁能给我任何关于如何调用此 API 或使用 Meteor 链接的提示?

更新:

这是我尝试过的代码,但没有显示任何响应

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 客户端范围的 block 中。因为您不能从客户端调用跨域(除非使用 jsonp),所以您必须将此 block 放在 Meteor.isServer block 中。

As an aside, per the documentation ,您的 checkTwitter 函数的客户端 Meteor.method 只是服务器端方法的 stub。您需要查看文档以获取有关服务器端和客户端 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
    });
}

https://stackoverflow.com/questions/14320610/

相关文章:

ruby-on-rails - 在rails中渲染json的最快方法是什么

php - 将 JSON 数据从 Javascript 发送到 PHP?

java - 如何使用 getJSONArray 方法访问 json 对象的嵌套元素

wcf - 将 Entity Framework 对象序列化为 JSON

javascript - 如何将 jQuery.serialize() 数据转换为 JSON 对象?

arrays - 如何在 json 模式中定义数组的最小大小

python - 将 json 字符串转换为 python 对象

c# - 创建 JObject 时出现参数异常

android - 在android中创建json

django - 如何使用 Django REST 框架制作 POST 简单的 JSON? CSRF