【发布时间】: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