Meteor.http.call 不允许 Access-Control-Allow-Origin [英] Meteor.http.call gives not allowed by Access-Control-Allow-Origin

查看:32
本文介绍了Meteor.http.call 不允许 Access-Control-Allow-Origin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用 Meteor.http.call("GET") 方法在 Meteor 中为 JSON 查询调用外部服务器时,我收到错误消息Access-Control-Allow 不允许-起源".

When I try to call an external server for JSON queries in Meteor with the Meteor.http.call("GET") method I get the error message "not allowed by Access-Control-Allow-Origin".

如何让我的流星应用程序对其他服务器进行 HTTP 调用?现在我在本地主机上运行它.

How do I allow my meteor app to make HTTP calls to other servers? Right now I run it on localhost.

我运行的代码是这样的:

The code I run is this:

Meteor.http.call("GET", 
                 "http://api.vasttrafik.se/bin/rest.exe/v1/location.name?authKey=XXXX&format=json&jsonpCallback=processJSON&input=kungsportsplatsen", 
                  function(error, result) {
                          console.log("test");    
                      }
                 );

推荐答案

StackOverflow 上还有其他类似的问题.

There are other questions similar to this on StackOverflow.

当您从客户端 (AJAX) 执行此操作时,您会受到尝试连接的服务器的限制.

You're restricted by the server you're trying to connect to when you do this from the client side (AJAX).

一种解决方法是,如果您可以访问外部服务器,则可以通过以下方式修改头文件以允许部分或全部来源:

One way to solve it is if you have access to the external server, you can modify the header file to allow some, or all origins by:

Access-Control-Allow-Origin: *

但是,如果您将调用放在服务器端并且不提供回调函数,则调用将同步进行,因此不会使用 AJAX,应该会成功.

However, if you place the call on the server side and not provide a callback function, the call will be made synchronously, thus not with AJAX, and it should succeed.

这里

Meteor.methods({checkTwitter: function (userId) {
   this.unblock();
   var result = Meteor.http.call("GET", "http://api.twitter.com/xyz", {params: {user: userId}});
   if (result.statusCode === 200) return true
   return false;
}}); 

这篇关于Meteor.http.call 不允许 Access-Control-Allow-Origin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆