44

これがTwitterAPIです。

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. Just Meteor:
    meteor add http

  2. 隕石:
    mrt add 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); result.data.response を返します。

HTTP.get にコールバックを渡さなかったので、応答が得られるまで待機しました。それが役に立てば幸い

于 2016-01-28T21:21:23.817 に答える