weixin_33674976 2017-03-06 15:59 采纳率: 0%
浏览 30

Flash NodeJS错误给用户?

I'm using Ajax and Node JS to let a user register on a one page application. When the user hits register I have it set up to check to see if the username is taken. All of this works so far. Now however, I am needing to alert the user that their username is taken. How would I print the error or flash the error to the user this way? The problem is I am avoiding page refreshes.

app.post("/quiz", function(req, res, next){
    User.findOne({username: req.body.username}, function(err, user){
       if(err) { console.log(err)} 
      if(user) { 

       console.log(user)
        // I NEED TO SEND THE ERROR HERE SAYING THE USERNAME IS TAKEN     
        // I NEED TO SEND THE ERROR HERE SAYING THE USERNAME IS TAKEN     
        // I NEED TO SEND THE ERROR HERE SAYING THE USERNAME IS TAKEN     
      }

    });

       //continue with your registration logic
       var newUser = new User({username: req.body.username, datapoint: req.body.datapoint});
         User.register(newUser, req.body.password, function(err, user){


        // if(res.error){

             if(err){
               req.flash("error", err.message);
               res.redirect('back')
               return res.render("quiz");

           } else {

           passport.authenticate("local")(req, res, function(){
           //   req.flash("success", "Welcome to JobQuiz " + user.username);
             res.redirect("jobquiz");
             console.log(req.body.datapoint)
           });
           }
       });



});

Ajax set-up

   $(function() {
                $("#checkpaymentForm").on("submit", function(e) {
                    e.preventDefault();
                    $.ajax({
                    url: $(this).attr("action"),
                   type: 'POST',
                   data: $(this).serialize(),
                   dataType:'json',
                })
                });
   });
  • 写回答

1条回答 默认 最新

  • weixin_33725807 2017-03-06 16:14
    关注

    First send your response in the if statement in the server code:

    app.post("/quiz", function(req, res, next){
        User.findOne({username: req.body.username}, function(err, user){
           if(err) { console.log(err)} 
           if(user) { 
                res.json({"message": "false" });
           }
    
     });
    

    Then in your front end add the success function to handle the response:

       $(function() {
                $("#checkpaymentForm").on("submit", function(e) {
                    e.preventDefault();
                    $.ajax({
                      url: $(this).attr("action"),
                      type: 'POST',
                      data: $(this).serialize(),
                      dataType: 'json',
                      success: function (data) {
                        if (data.message === "false") {
                            //TODO: alert users here
                        }
                     })
                });
       });
    
    评论

报告相同问题?

悬赏问题

  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择