【问题标题】:No 'Access-Control-Allow-Origin' header on requested resource: Meteor server-side请求的资源上没有“Access-Control-Allow-Origin”标头:Meteor 服务器端
【发布时间】:2016-03-10 20:08:03
【问题描述】:

我在这里问了一个问题:https://stackoverflow.com/questions/34097411/angular-meteor-enabling-cors-for-client-side-http-services 从那时起,我了解到我应该能够通过从服务器端进行调用来解决名义上的错误。我已经这样做了,即使使用 access-control-origin 标头,我仍然收到 “XMLHttpRequest 无法加载”和 “因此,不允许访问来源 'http://localhost:3000'” 在我联系 API 提供商后,我还使用了直接从 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 请求访问的知识,所以我很难过上面没有解决这个问题,唉:

How to make an API call using meteor

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

【问题讨论】:

    标签: http meteor xmlhttprequest cors


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2017-02-07
      • 2023-03-16
      • 2014-07-30
      • 2016-01-14
      • 2023-03-15
      • 2018-12-02
      • 2015-10-30
      • 2019-10-07
      • 2021-09-28
      相关资源
      最近更新 更多