2015-06-22 131 views
-1

我面临下面的问题,我打了一个文件在不同的服务器上,从7777是3000.平台是node.js?XMLHttpRequest,请求的资源中没有访问控制 - 允许来源

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

的您的node.js脚本中不允许跨域请求。 –

回答

-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); 
+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); 
+0

谢谢mohit。它的工作,但什么是“P3P”。最后两个iines –

+0

好的,再次感谢 –

+1

@DineshRawat你应该接受为答案,如果有帮助,这样可以帮助其他用户 – user3819192

相关问题