Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor.http.call gives not allowed by Access-Control-Allow-Origin

Tags:

meteor

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".

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");    
                      }
                 );
like image 987
Anders Thid Avatar asked Sep 20 '12 07:09

Anders Thid


1 Answers

There are other questions similar to this on StackOverflow.

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: *

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.

Here's

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;
}}); 
like image 153
zkokaja Avatar answered Oct 17 '22 22:10

zkokaja