请求的资源上没有“Access-Control-Allow-Origin”标头:Meteor服务器端

时间:2015-12-06 01:27:57

标签: http meteor xmlhttprequest cors

我在这里问了一个问题:https://stackoverflow.com/questions/34097411/angular-meteor-enabling-cors-for-client-side-http-services 从那时起我就知道我应该能够通过从服务器端进行调用来解决名义错误。我已经这样做了,即使使用access-control-origin标头,我仍然接收 “XMLHttpRequest无法加载”和 因此,“原始'http://localhost:3000'不允许访问” 在我联系它们之后,我也正在使用直接从API提供商提供给我的示例查询。

这是我在服务器上的内容:

if(Meteor.isServer){
Meteor.methods({
'inFetch': function(){
  var method = 'GET';
  var url = "http://food2fork.com/api/search?     
key=[thiskey]&q=carrot";
  var options = {
    headers: {
      'Access-Control-Allow-Origin': '*',
          'Access-Control-Allow-Methods': 'POST, PUT, DELETE, GET, OPTIONS',
          'Access-Control-Request-Method': '*',
          'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-         
Type, Accept, Authorization'
    }
  }
this.unblock();
Meteor.http.call(method, url, options, function(error, result){
if (error) {
     console.log('SERVER ERRR');
     return "error";
   } else{
     console.log('SERVER RESULT');
     return result;
 }
});

}
});

这就是我在客户端上的所有内容:

function inFetch(){
  // food2fork get //
  $scope.inHits=[];
  $scope.preInHits=[];
  $scope.inHits= Meteor.call("inFetch");

}

我一直在研究这个问题已经很长时间了,并且学到了很多关于HTTP请求访问的知识,所以我很难以解决上面的问题,ala:

How to make an API call using meteor

编辑:删除了原始问题的链接

1 个答案:

答案 0 :(得分:1)

好的,我发现了问题。虽然我仍然需要解析它并将JSON获取到我的客户端页面,但我已使用上述结构登录到控制台成功的API响应。问题是我的代码中的其他地方的错误。如果有人想知道如何处理与Meteor讨厌恼人的CORS,希望你能找到这篇文章。