在NodeJS/Express中使用Passport嘗試使用Facebook對用戶進行身份驗證時,已有兩天,一百萬次嘗試啓用CORS。Angular/Node/Express/Passport跨域問題 - 啓用CORS Passport Facebook身份驗證
我得到在Chrome的錯誤是這樣的:
XMLHttpRequest cannot load https://www.facebook.com/dialog/oauth?response_type=code&redirect_uri=http%…%3A8080%2Fauth%2Ffacebook%2Fcallback&scope=email&client_id=598171076960591.
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://localhost:8080' is therefore not allowed access.
我使用的路線是那樣簡單:
// =====================================
// FACEBOOK ROUTES =====================
// =====================================
// route for facebook authentication and login
app.get('/auth/facebook', passport.authenticate('facebook', { scope : 'email' }));
// handle the callback after facebook has authenticated the user
app.get('/auth/facebook/callback',
passport.authenticate('facebook', {
successRedirect : '/home',
failureRedirect : '/login'
}));
這是路線怎麼叫我angularJS文件(我「以前也嘗試設置withCredentials:TRUE):
$http.get('/auth/facebook')
.success(function(response) {
}).error(function(response){
});
我已經嘗試了十幾個方案,我發現這裏在StackOverflow和其他論壇上。
我試圖在routes.js文件我之前航線上增加這樣的:
app.all('*', function(req, res, next) { res.header('Access-Control-Allow-Origin', '*'); res.header("Access-Control-Allow-Headers", "Content-Type,X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Date, X-Api-Version, X-File-Name"); res.header('Access-Control-Allow-Methods', 'GET,POST,PUT,HEAD,DELETE,OPTIONS'); res.header('Access-Control-Allow-Credentials', true); if ('OPTIONS' == req.method) { res.send(200); } else { next(); } });
我試着server.js文件中添加這種(請注意,我改變了頭,但的setHeader我已經試過這兩種):
app.use(function(req, res, next) { res.setHeader('Access-Control-Allow-Origin', '*'); res.setHeader('Access-Control-Allow-Headers', 'Content-Type,X-Requested-With'); res.setHeader('Access-Control-Allow-Methods', 'GET,POST,PUT,HEAD,DELETE,OPTIONS'); res.setHeader('Access-Control-Allow-Credentials', true); if ('OPTIONS' == req.method) { res.send(200); } else { next(); } }); require('./app/routes.js')(app, passport);
我試圖在我的app.js文件中添加這個(angularJS配置):
$httpProvider.defaults.useXDomain = true; delete $httpProvider.defaults.headers.common['X-Requested-With']; $httpProvider.defaults.withCredentials = true;
無論如何,我不知道還有什麼要做。我在網上找到的所有東西都沒用。 有沒有可能與我使用AngularJS Routing有關?我看不出有什麼理由爲什麼這很重要,但我有點用不着猜測。
我的情況非常相似,這一個:提前 Angular/Node/Express/Passport - Issues when connecting to facebook(CORS)
謝謝!
可能重複[?如何允許快遞/ CORS的NodeJS(http://stackoverflow.com/questions/7067966/how-to-allow-cors-in-express -nodejs) – 2014-09-25 06:52:18
我知道有很多類似的問題,我幾乎看到了所有這些問題。不幸的是,迄今爲止我所嘗試的解決方案都沒有奏效...... – 2014-09-25 10:59:06
您不能在同一時間同時使用'訪問控制 - 允許 - 憑證:true'和'Access-Control-Allow-Origin:*''。對於你的情況,你應該設置'訪問控制 - 允許 - 原產地:本地主機:8080'而不是'*'。 – wuct 2015-07-15 14:23:27