gpt4 book ai didi

ajax - 使用 CORS 登录expressjs 应用程序

转载 作者:太空宇宙 更新时间:2023-11-04 01:05:09 25 4
gpt4 key购买 nike

我有expressjs坐在nodejs服务器上,我有一个客户端cordova应用程序向某些路由发出ajax请求。

这很好,直到我需要使用 Passportjs 发出 POST 请求登录时,会发生 302 重定向,因此在进行此调用时我收到此302 Moved Temporarily

$('body').on('submit', '#logIn', function(e){
e.preventDefault();

var formData = $(this).serialize();

$.ajax({
url: "http://mydomain.io:3300/login",
data: JSON.stringify(formData),
type: "POST",
crossDomain: true,
dataType: "json",
async: true,
success: function(response){
alert('succeeded!');
console.log(response);
alert(response);
},
failure: function(message){
alert("failed");
console.log(message);
alert(message);
}
});

});

所以我的问题是如何使用 CORS 使用客户端 ajax 登录应用程序?

最佳答案

CORS 不是你的问题。

Passport 希望重定向您的用户(基于您传递给 passport.authenticate 的值)。例如:

app.get('/auth/facebook/callback',
passport.authenticate('facebook', { successRedirect: '/',
failureRedirect: '/login' }));

Passport 将通过返回 302 告诉浏览器重定向到 //login。您可以通过删除 passport.authenticate 的第二个参数来删除重定向:

app.get('/auth/facebook/callback',
passport.authenticate('facebook'));

这将在身份验证成功时调用 next()(否则返回 401)。

此处的示例使用 FacebookStrategy,但它适用于任何策略。

关于ajax - 使用 CORS 登录expressjs 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23716320/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com