res.redirected 即使后端 returns 重定向也返回 false
res.redirected returning false even when the backend returns a redirect
我正在尝试创建一个登录页面,该页面在使用 nodejs 和 fetch 成功登录后将用户重定向回主页。
这是后台代码。
app.post('/login',(req,res)=>
{
user.findOne(req.body,(err,info)=>{
if(err)
{
res.json(`error in finding item in database ,error info ${err}`);
}
else if(!info)
{
console.log('user doesnot exist');
let message='*please enter a valid username and password';
res.json(message);
}
else{
console.log(info);
res.redirect('/');
}
});
});
这是前端代码
form.addEventListener('submit',(e)=>{
e.preventDefault();
const username=document.getElementById('username').value.trim();
const password=document.getElementById('password').value;
fetch('/login',{
method:'POST',
headers:{
'Content-Type':'application/json'
},
redirect:"manual",
body:JSON.stringify({username,password})
}).then((res)=>{
if(res.redirected)
{
console.log(`redirected to ${res.url}`);
window.location.href=res.url;
// return;
}
else{
console.log("no")
return res.json()
}
})
.then((data)=>{
if(data)
alt.innerText=data;//showing the error on the page
})
.catch((err)=>{throw err})
})
当我输入正确的凭据时,它会在控制台上打印信息,这意味着它在 else 内,理想情况下应该重定向回主页。但它永远不会进入 if(res.redirected) 语句,这意味着它返回 false.
由于某些较旧的 chrome 版本不读取 res.redirected
,并且为了实现更清晰的代码以及意外的重定向(例如 HTTP 到 HTTPS),您可能需要设置 res.direct('/')
改为res.json({ redirect: '/' })
。
const results = await fetch(....).then(res.json());
if (results.redirect) {
window.location.href = results.redirect
}
if (results.data) {
//do something
}
后端
if (info) {
return res.json({ data: info });
} else {
return res.json({ redirect: '/', error: true })
}
我正在尝试创建一个登录页面,该页面在使用 nodejs 和 fetch 成功登录后将用户重定向回主页。 这是后台代码。
app.post('/login',(req,res)=>
{
user.findOne(req.body,(err,info)=>{
if(err)
{
res.json(`error in finding item in database ,error info ${err}`);
}
else if(!info)
{
console.log('user doesnot exist');
let message='*please enter a valid username and password';
res.json(message);
}
else{
console.log(info);
res.redirect('/');
}
});
});
这是前端代码
form.addEventListener('submit',(e)=>{
e.preventDefault();
const username=document.getElementById('username').value.trim();
const password=document.getElementById('password').value;
fetch('/login',{
method:'POST',
headers:{
'Content-Type':'application/json'
},
redirect:"manual",
body:JSON.stringify({username,password})
}).then((res)=>{
if(res.redirected)
{
console.log(`redirected to ${res.url}`);
window.location.href=res.url;
// return;
}
else{
console.log("no")
return res.json()
}
})
.then((data)=>{
if(data)
alt.innerText=data;//showing the error on the page
})
.catch((err)=>{throw err})
})
当我输入正确的凭据时,它会在控制台上打印信息,这意味着它在 else 内,理想情况下应该重定向回主页。但它永远不会进入 if(res.redirected) 语句,这意味着它返回 false.
由于某些较旧的 chrome 版本不读取 res.redirected
,并且为了实现更清晰的代码以及意外的重定向(例如 HTTP 到 HTTPS),您可能需要设置 res.direct('/')
改为res.json({ redirect: '/' })
。
const results = await fetch(....).then(res.json());
if (results.redirect) {
window.location.href = results.redirect
}
if (results.data) {
//do something
}
后端
if (info) {
return res.json({ data: info });
} else {
return res.json({ redirect: '/', error: true })
}