Javascript 如何使用meteor进行API调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14320610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 16:22:20  来源:igfitidea点击:

How to make an API call using meteor

javascriptjsonapitwittermeteor

提问by iJade

Ok here is the twitter API,

好的,这里是推特 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

任何人都可以给我任何关于如何使用Meteor调用此 API 或链接的提示吗?

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

回答by TimDog

You are defining your checkTwitter Meteor.methodinsidea client-scoped block. Because you cannot call cross domain from the client (unless using jsonp), you have to put this block in a Meteor.isServerblock.

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

As an aside, per the documentation, the client side Meteor.methodof your checkTwitter function is merely a stubof a server-side method. You'll want to check out the docs for a full explanation of how server-side and client-side Meteor.methodswork together.

Meteor.method顺便说一句,根据文档,您的 checkTwitter 函数的客户端仅仅是服务器端方法的存根。您需要查看文档以获得有关服务器端和客户端如何Meteor.methods协同工作的完整说明。

Here is a working example of the http call:

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

回答by user2132316

This might seem rudimentary - but the HTTP package does not come by default in your Meteor project and requires that you install it a la carte.

这可能看起来很初级 - 但是 HTTP 包在您的 Meteor 项目中不是默认提供的,并且要求您按菜单安装它。

On the command line either:

在命令行上:

  1. Just Meteor:
    meteor add http

  2. Meteorite:
    mrt add http

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

  2. 陨石:
    mrt 添加http

Meteor HTTP Docs

流星 HTTP 文档

回答by Lander Van Breda

Meteor.http.get on the client is async, so you will need to provide a callback function :

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

Meteor.http.call("GET",url,function(error,result){
     console.log(result.statusCode);
});

回答by Rahul

Use Meteor.http.get. Per the docs:

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

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

The docs actually include some examples of using Twitter, so you should be able to get started with them.

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

回答by Mr Megamind

on server side if you provide the call back to http.get it will be asynch call so my solutions to that undefined return on client was

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

var result = HTTP.get(iurl); return result.data.response;

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

as i did not pass a call back to HTTP.get so it waited until i got response. hope it helps

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