Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Meteor 流星获得请求_Meteor_Get - Fatal编程技术网

Meteor 流星获得请求

Meteor 流星获得请求,meteor,get,Meteor,Get,请容忍我的任何错误/术语错误,因为我对这一切都是新手。我正在使用meteor开发我的项目,我需要向外部API发出get请求。我已经添加了meteor add http,下面是我的代码: HTTP.call( 'GET', 'url', {}, function( error, response ) { if ( error ) { console.log( error ); } else { console.log( response ); } }); 如果我在Me

请容忍我的任何错误/术语错误,因为我对这一切都是新手。我正在使用meteor开发我的项目,我需要向外部API发出get请求。我已经添加了meteor add http,下面是我的代码:

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

如果我在Meteor中使用客户端文件夹中的代码,我会收到以下错误:请求的资源上不存在“Access Control Allow Origin”标题。起源'http://localhost:3000因此不允许访问meteor它与CORS有关,我不知道如何实现。如果我在服务器端使用上面的代码,我会在控制台中得到正确的响应,但如何将其用作客户端javascript代码的变量?

修复了它。在客户端

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

Tou可以使用HTTP的.call函数,并在选项中传递头:

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

方法字符串

url字符串

异步回调函数

选择权

内容字符串

数据对象

查询字符串

参数对象

验证字符串

标题对象

超时数

follow重定向布尔值

npmRequestOptions对象

发送前功能


来源:

启用CORS取决于服务提供商,但有一些方法可以尝试绕过它,但这并不漂亮。我建议您只需从Meteor方法中的服务器执行调用,然后从Meteor客户端代码调用该方法。简单明了。请注意,这会造成一个安全漏洞,任何人都可以让您的服务器对任何url进行任意次数的http调用。
The HTTP method to use, such as "GET", "POST", or "HEAD".
The URL to retrieve.
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.
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.
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.
If true, transparently follow HTTP redirects. Cannot be set to false on the client. Default true.
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.
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.