Emmanuel Amodu Emmanuel Amodu - 2 months ago 12
HTTP Question

Having issues getting page title from html with meteor.http package

Meteor.http.call( 'GET', 'http://google.com', {}, function( error, response ) {
if ( error ) {
console.log( error );
} else {
console.log( response );
}
});


the problem is it keeps showing this error this is my first time using this package so am not sure if i really understand it.

this is the error on my console.


XMLHttpRequest cannot load http://google.com. No
'Access-Control-Allow-Origin' header is present on the requested
resource. Origin 'http://localhost:3000' is therefore not allowed
access.

Answer

HTTP Requests from the browser will always run into this CORS issue unless you specifically allow them with CORS headers.

Meteor has a good way of dealing with it. First up you do a call:

Meteor.call("httpRequest","http://myserver.com/path/to/file",params);

In the server you write a Meteor method like this

Meteor.methods({
    httpRequest: function(url,params) {
    // Send the http request here
    })
});

You cannot do a call back to the client with the result of the http request, but you can put it into a database record, which the client subscribes to.