2015-06-22 124 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

相關問題