简体   繁体   English

从express渲染.pug文件后视图未更改

[英]View is not change after render .pug file from express

I have some problem that the html view is not change after render the pug file,after getting the server response. 我有一些问题,在得到服务器响应后,渲染pug文件后html视图不会改变。

The code is following 代码如下

app.set('view engine', 'pug');
 app.set("views", require('path').join(__dirname, "views"));

app.post('/login', function(req, res, next) {
console.log(req.body);
checkExistanceOfuser(req.body.username, req.body.password, function(err, flag, data) {
    console.log(err);
    if (err) {
        res.send({
            err: 'SOME_DATABASE_ERROR'
        })
    } else {
        if (flag) {

            req.session.user = data;

            /*res.send({
                success: true,
                message: 'Login Success'
            })*/

            res.render('welcome', { name: data.name });
        } else {
            /*res.send({
                success: false,
                message: 'Invalid Credentials'
            });*/
            res.render('login', { error: 'Invalid Credentials' });
        }
    }
})

But i check in network section from browser.The API response (preview) is fine. 但是我从浏览器中检查网络部分.API响应(预览)很好。

You're rendering views/login , but you've already specified that the view folder is views . 您正在渲染views/login ,但是已经指定view文件夹为views Just render login . 只需渲染login

when you are calling /login route it's a post call and probably you are using ajax post call for doing so. 当您调用/login route时,这是一个后期调用,可能您使用的是ajax后期调用。 Now when you are calling the /login route its rendering the pug file but its not actually effecting the browser DOM. 现在,当您调用/login路由时,它将呈现pug文件,但实际上并没有影响浏览器DOM。 So what you need to do is this 所以你需要做的是

create a new route like this 创建这样的新路线

  app.get('/login-success', function(req, res, next) {
        if (req.session && req.session.user) {
            res.render('welcome', { name: req.session.user.name });
        } else {
        res.redirect('/');
        }
  });

and modify the login function like this 并像这样修改登录功能

   app.post('/login', function(req, res, next) {
      console.log(req.body);
      checkExistanceOfuser(req.body.username, req.body.password, function(err, flag, data) {
      console.log(err);
      if (err) {
         res.send({
            err: 'SOME_DATABASE_ERROR'
         })
      } else {
            if (flag) {

                req.session.user = data;

                res.send({
                    success: true,
                    message: 'Login Success'
                });
             } else {
                res.send({
                success: false,
                message: 'Invalid Credentials'
                });
             }
          }
      })
  });

in ajax call use this 在ajax调用中使用此

function login(){
  $.post("http://localhost:8000/login",
  {
    username:document.getElementById("name").value,
    password:document.getElementById("password").value,
  },
  function(data, status){
     if(data.success){
      window.location = "http://localhost:8000/login-success"
     }
  });
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用express重新渲染哈巴狗视图 - re-render pug view with express 在 express 中渲染 JSON 文件以在 PUG 中插值 - Render JSON file in express to be interpolated in PUG 使用Express 4更改URL以在何处呈现视图 - change url where to render view with Express 4 Express:forEach完成后渲染视图 - Express : Render view after forEach gets completed "<i>How do I render a pug template on success and failure of authentication(passport.js) with variables from the pug template?<\/i>如何使用哈巴狗模板中的变量在身份验证(passport.js)成功和失败时呈现哈巴狗模板?<\/b> <i>(express, passport.js, pug)<\/i> (快递,passport.js,哈巴狗)<\/b>" - How do I render a pug template on success and failure of authentication(passport.js) with variables from the pug template? (express, passport.js, pug) 快速渲染视图并同时下载文件 - Express render view and download file at the same time 在Express JS中,如何在Middlware之后呈现静态文件不在公共或查看文件夹中 - How to render static file not in public or view folder after middlware in express js (Node.js/Express) 将数据从控制器发送到 pug 视图然后由 Javascript 处理时的最佳实践 - (Node.js/Express) Best practice when sending data from controller to pug view which is then processed by Javascript 带有 JSON 文件的 Express 框架中的 PUG 多次迭代 - PUG multiple Iteration in Express Framework with JSON file 无法在Express Pug中链接JavaScript文件 - Unable to link javascript file in express pug
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM