res.redirect不会将页面重新加载到新端点

编程入门 行业动态 更新时间:2024-10-12 05:47:34

res.redirect不<a href=https://www.elefans.com/category/jswz/34/1770640.html style=会将页面重新加载到新端点"/>

res.redirect不会将页面重新加载到新端点

我正在编写测验应用程序的登录页面。按下登录按钮后,我正在向服务器发送发布请求,服务器将重定向到列出问题的主页。问题在于它仅刷新登录页面。

  // FRONT-END code
  const username = document.getElementById('username').value;
  const password = document.getElementById('password').value;

  const xhr = new XMLHttpRequest();

  // I only put this in to reload the page somehow
  xhr.onreadystatechange = () => {
    window.location.href = '/';
  };

  xhr.open('POST', '/login');
  xhr.setRequestHeader('Content-Type', 'application/json');
  xhr.send(JSON.stringify({
    username,
    password,
  }));
// SERVER-SIDE code
router.post('/login', jsonParser, (req, res) => {
  if (req.body.username === undefined || req.body.password === undefined) {
    console.log('Doesn\'t have neccesarry elements');
    res.status(400).render('error', { message: 'Doesn\'t have necessary elements' });
  } else {
    ...
    res.redirect('/');
  }
});

我想提一下/也是模板渲染页面,也许是问题所在?

回答如下:

问题出在您的前端,如果客户端使用xml请求服务器请求,则不会重定向页面。因此,您必须在xml请求之后从客户端window.location.href重定向。

1。如果这样,在向{status: error}发送服务给客户时出错,2.检查客户端中的状态是否错误,请更改clinen中的路径。

更多推荐

res.redirect不会将页面重新加载到新端点

本文发布于:2024-05-07 13:51:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1756544.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:会将   到新   加载   页面   res

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!