Javascript Passport Facebook策略,使用AJAX触发路由
问题是: 无法加载XMLHttpRequesthttps://www.facebook.com/dialog/oauth?response_type=code&redirect_uri=http%…lhost%3A8080%2API%2Auth%2Facebook%2Callback&client_id=1527429390857121。请求的资源上不存在“Access Control Allow Origin”标头。起源'http://localhost:8080因此,不允许访问 如果我单击错误中的链接,我将获得autenticate并返回json格式的数据。如果我删除客户端js路由器,一切正常。但我正在尝试建立一个水疗应用程序 我不使用任何特定的前端框架,我使用路由器并使用手柄生成视图 在客户端:Javascript Passport Facebook策略,使用AJAX触发路由,javascript,node.js,express,facebook-authentication,passport.js,Javascript,Node.js,Express,Facebook Authentication,Passport.js,问题是: 无法加载XMLHttpRequesthttps://www.facebook.com/dialog/oauth?response_type=code&redirect_uri=http%…lhost%3A8080%2API%2Auth%2Facebook%2Callback&client_id=1527429390857121。请求的资源上不存在“Access Control Allow Origin”标头。起源'http://localhost:8080因此,不允许访问 如果我单击错
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);
...
这似乎是一个cors问题: 请尝试以下操作: $npm安装cors 然后,在server.js中: var cors=要求“cors”; ... app.usecors 有关cors模块的更多详细信息:
在cors和ajax上:尝试将其添加到服务器:
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
next();
});
你好,不幸的是我仍然得到相同的错误。如果我打开前端路由,在没有ajax的情况下直接点击url,它会很有魅力。已经有一段时间了,但是你运气好吗?你找到解决办法了吗?