【发布时间】:2023-03-06 15:40:02
【问题描述】:
问题:
XMLHttpRequest cannot load https://www.facebook.com/dialog/oauth?response_type=code&redirect_uri=http%…lhost%3A8080%2Fapi%2Fauth%2Ffacebook%2Fcallback&client_id=1527429390857121. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access.
如果我单击错误中的链接,我将获得身份验证并取回 json 中的数据。如果我删除客户端 js 路由器,一切正常。但我正在尝试构建一个 SPA 应用程序。
我不使用任何特定的前端框架,我使用flatiron director 路由器并使用把手生成视图。
在客户端:
var facebook = function () {
console.log('GET /auth/facebook');
$.ajax({
url: '/api/auth/facebook',
type: 'get',
dataType: 'jsonp',
cache: false
});
};
// ROUTES ===============================
var routes = {
...
'/auth': {
'/facebook' : facebook,
'/twitter' : twitter,
'/google': google
},
...
};
在服务器端:
// send to facebook to do the authentication
router.get('/auth/facebook', passport.authenticate('facebook'));
// handle the callback after facebook has authenticated the user
router.get('/auth/facebook/callback', function(req, res, next) {
passport.authenticate('facebook', function (err, user, info) {
if (err) return next(err);
if (!user) return res.status(403).json(info);
req.logIn(user, function (err) {
if (err) { return next(err); }
return res.json({user: user, message: info});
});
})(req, res, next);
});
在 server.js 中
...
app.use('/api', router);
...
【问题讨论】:
-
已经有一段时间了,但是你有什么运气吗?你找到解决办法了吗?
标签: javascript node.js express facebook-authentication passport.js