Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
117
rated 0 times [  122] [ 5]  / answers: 1 / hits: 109568  / 11 Years ago, tue, april 16, 2013, 12:00:00

I'm trying to do a fetch from backbone.js to my node.js server. However, I get the following error in the console:



Origin http://localhost is not allowed by Access-Control-Allow-Origin.



I added the following to my node.js server:



var allowCrossDomain = function(req, res, next) {
res.header('Access-Control-Allow-Origin', http://localhost);
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type');
};

app.configure(function() {
app.use(allowCrossDomain);
});


But it's still returning the same error. However, even if this did work, it doesn't seem like the ideal solution, as I would like users from all over to be able to send requests.


More From » node.js

 Answers
72

If you want everyone to be able to access the Node app, then try using



res.header('Access-Control-Allow-Origin', *)


That will allow requests from any origin. The CORS enable site has a lot of information on the different Access-Control-Allow headers and how to use them.



I you are using Chrome, please look at this bug bug regarding localhost and Access-Control-Allow-Origin. There is another StackOverflow question here that details the issue.


[#78864] Monday, April 15, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
clarkulisesa

Total Points: 422
Total Questions: 93
Total Answers: 112

Location: Austria
Member since Thu, Jan 7, 2021
3 Years ago
clarkulisesa questions
Mon, Feb 24, 20, 00:00, 4 Years ago
Mon, Aug 12, 19, 00:00, 5 Years ago
;