Nodejs external server error connection

时间:2015-11-12 10:48:17

标签: angularjs node.js http

I have one main server which have all files and information and i have build another nodejs project to serve images. When i am trying to connect with it i see XMLHttpRequest cannot load http://localhost:2000/addImageURL.The 'Access-Control-Allow-Origin' header has a value 'http://localhost:8754' that is not equal to the supplied origin. Origin 'http://127.0.0.1:8754' is therefore not allowed access..
To connect i am using angular with $http. On the server i have white listed my 8754 port like below.

app.use(function (req, res, next) {
    res.setHeader('Access-Control-Allow-Origin', 'http://localhost:8754');
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
    res.setHeader('Access-Control-Allow-Credentials', true);
    next();
});

Update

If i modify all urls to 127.0.0.1 then i get below error: XMLHttpRequest cannot load http://127.0.0.1/addImageURL. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:8754' is therefore not allowed access.

1 个答案:

答案 0 :(得分:1)

试试这个

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

如果有效,则表示您为跨源'http://127.0.0.1:8754'提供的网址错误。