我面对下面的问题,我在7777的另一台服务器上命中了一个文件。平台是node.js吗?
XMLHttpRequest cannot load http://localhost:3000/register_user. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:7777' is therefore not allowed access.
提前致谢!
答案 0 :(得分:0)
您可以通过添加Access-Control-Allow-Origin来通过HTTP标头控制此操作。将其设置为Access-Control-Allow-Origin: *
将接受来自任何域的跨域AJAX请求。
尝试在app.js文件中添加此内容
var allowCrossDomain = function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
res.header('Cache-Control', 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0');
next();
};
然后像这样使用: -
app.use(allowCrossDomain);
答案 1 :(得分:-2)
我只需要一个域
var allowCrossDomain = function (req, res, next) {
res.header('Access-Control-Allow-Origin', req.headers.orgigin);
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
res.header('Cache-Control', 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0');
next();
};
app.use(allowCrossDomain);