2017-02-28 7 views
0

私はこのすべてに新しいので、任意の間違い/間違った用語のために私を持っています。私は私のプロジェクトを開発するために流星を使用しています。私は外部のAPIに要求を出す必要があります。以下は、(私はすでにmeteor add httpを追加しました)私のコードです:流星リクエストを取得

HTTP.call('GET', 'url', {}, function(error, response) { 
    if (error) { 
    console.log(error); 
    } else { 
    console.log(response); 
    } 
}); 

私は流星で私のクライアントのフォルダ内のコードを使用している場合は、私は次のようなエラーNo 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access meteorを得ることがどのように私は理解してなかったCORSとは何かを持っています実装する。上記のコードをサーバー側で使用すると、コンソールで正しい応答が得られますが、クライアントのjavascriptコードでvarとしてどのように使用するのですか?

+0

その、しかし、あなたは(それを回避ハッキングを試みることができる方法がありますしかしそれはかなりではない)。 Meteorメソッド内からサーバーから呼び出しを行い、Meteorクライアントコードからメソッドを呼び出すことをお勧めします。簡単で、簡単で、簡単です。 – jordanwillis

答えて

0

修正済みです。クライアント側では

Meteor.call("getURL",'url',{},function(err,res){ 
    if(err){ 
     console.log('Error: '+err); 
    } 
    if(!err){ 
    console.log('Response: '+res); 
     } 

とサーバの塔を使用することができます

Meteor.methods({ 
    'getURL': function(url_l){ 
    console.log("Request: "+url_l) 
    return HTTP.get(url_l) 
    } 
}); 
+2

これは誰でもあなたのサーバーが* any * urlにhttp呼び出しを何回も行うことができるセキュリティホールを作成することに注意してください。 –

0

に。HTTPの関数を呼び出すとオプションであなたのヘッダーを渡す:

HTTP.call(method, url, [options], [asyncCallback]) 

引数

方法文字列

The HTTP method to use, such as "GET", "POST", or "HEAD". 

URL文字列

The URL to retrieve. 

asyncCallback機能

Optional callback. If passed, the method runs asynchronously, instead of synchronously, and calls asyncCallback. On the client, this callback is required. 

オプション

コンテンツストリング

String to use as the HTTP request body. 

データオブジェクト

JSON-able object to stringify and use as the HTTP request body. Overwrites content. 

クエリストリング

Query string to go in the URL. Overwrites any query string in url. 

paramsはオブジェクト

Dictionary of request parameters to be encoded and placed in the URL (for GETs) or request body (for POSTs). If content or data is specified, params will always be placed in the URL. 

AUTHストリング

HTTP basic authentication string of the form "username:password" 

ヘッダオブジェクト

Dictionary of strings, headers to add to the HTTP request. 

タイムアウト

Maximum time in milliseconds to wait for the request before failing. There is no timeout by default. 

followRedirectsブール

If true, transparently follow HTTP redirects. Cannot be set to false on the client. Default true. 

npmReq uestOptionsオブジェクト

On the server, HTTP.call is implemented by using the npm request module. Any options in this object will be passed directly to the request invocation. 

beforeSend機能

On the client, this will be called before the request is sent to allow for more direct manipulation of the underlying XMLHttpRequest object, which will be passed as the first argument. If the callback returns false, the request will be not be send. 

源泉:CORSを有効にするには、サービスプロバイダまでHere