原产地http:// localhost不被Access-Control-Allow-Origin允许 [英] Origin http://localhost is not allowed by Access-Control-Allow-Origin

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

问题描述

我正在尝试从backbone.js取回到我的node.js服务器。但是,我在控制台中收到以下错误:



原始http:// localhost不允许Access-Control-Allow-Origin。



我添加了以下到我的node.js服务器:

  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);
});

但它仍然返回相同的错误。但是,即使这样工作,它似乎不是理想的解决方案,因为我希望来自各地的用户能够发送请求。

解决方案

如果您希望每个人都能访问Node应用程序,请尝试使用

  res。 


$ b 这将允许请求任何来源。 CORS启用网站有许多有关不同的Access-Control-Allow标头以及如何使用它们的信息。



我正在使用Chrome,请查看此< a>关于localhost和Access-Control-Allow-Origin的bug。此处还有另一个 StackOverflow问题详细描述了该问题。 / p>

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.

解决方案

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.

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

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