2016-05-02 31 views
0

我有一个表格:如何在节点中为用户创建某种警报?

<form class="form-inline" action="/" method="post"> 
    <div class="form-group"> 
     <label class="sr-only">Username</label> 
     <input name="username" type="text" class="form-control" placeholder="Desired Username"> 
    </div> 
    <br> 
    <br> 
    <div class="form-group"> 
     <label class="sr-only" for="exampleInputEmail3">Email address</label> 
     <input name="email" type="email" class="form-control" id="exampleInputEmail3" placeholder="Email"> 
    </div> 
    <br> 
    <br> 
    <div class="form-group"> 
     <label class="sr-only" for="exampleInputPassword3">Password</label> 
     <input name="password" type="password" class="form-control" id="exampleInputPassword3" placeholder="Password"> 
    </div> 
    <br> 
    <br> 
    <button type="submit" class="btn btn-default">Register</button> 
</form> 

这里是我的app.js:

app.post('/', function(req, res) { 

    var user = new Parse.User(); 
    user.set("username", req.body.username); 
    user.set("password", req.body.password); 
    user.set("email", req.body.email); 

    user.signUp(null, { 
    success: function(user) { 
     // Hooray! Let them use the app now. 
     console.log(user); 
     res.redirect('/#register'); 
    }, 
    error: function(user, error) { 
     // Show the error message somewhere and let the user try again. 
     console.log(error.message); 
     res.redirect('/#register'); 

    } 
    }); 

}); 

一切功能的作品,我只是想知道我怎么可以显示一个消息,如果用户已经成功签约或不是。我还想知道这是否是处理表单的正确方法。

+0

你应该叫'res.send'从那里发送成功消息前端和处理重定向。 –

+0

你能举个例子吗? – user2975275

+0

检查我的答案。 –

回答

0

处理注册后的成功状态。

success: function(user) { 
    // Hooray! Let them use the app now. 
    res.redirect('/#register'); 
} 

成功注册后,您将重定向到/#注册页面。您可以在加载此页面时显示警报。

+0

我试过alert();但它不起作用 – user2975275

+0

'alert'在服务器端不存在,它只存在于浏览器中。 –

+0

但我的功能在服务器端。我怎么能仍然警惕? – user2975275

0

前端JS:

而是提交表单通常的方式,我们将使用jQuery-AJAX用于此目的:


使用下面的<script>标签附加jQuery到您的网页在您的网页上<head>标记:

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.3/jquery.min.js"></script> 

跟着它了在同一<head>有以下几点:

<script type="text/javascript"> 

$('.form-inline').submit(function(event) { 

    $.ajax({ 
    type: "POST", // the request type 
    url: "/", // the URL where your form data will be sent 
    data: $(this).serialize(), // the form data to be sent 
    success: function(data) { // this function will be called when you do a res.send from the back-end 

     alert(data); // although I would suggest you use a modal or some other solution to display your success message 

     window.location.href = your_redirect_url; // this will redirect the user to your /#register url 

    } 
    }); 

    event.preventDefault(); // to prevent the form from being submitted the usual way 

}); 

</script> 

注:这仅仅是一个例子,以帮助您开始。


后端JS:

 
app.post('/', function(req, res) { 

    ... 

    user.signUp(null, { 
    success: function(user) { 

     res.send(your_success_message); // send your success message or status or result object using this method, it will call the success function of your ajax object on the front-end 
    }, 
    error: function(user, error) { 

     res.send(your_error_message); // similarly send the error message, or status, or object. 
    } 
    }); 
}); 
+0

没有Ajax可以做到这一点?像我可以做到只有简单的节点和HTML? – user2975275

+0

如果您不想使用Ajax,那么您将需要恢复为本地本地的[XMLHttpRequest](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest)函数在浏览器的Javascript中。但是Ajax使用起来更简单。 –

+0

但是,如果您想在窗体的同一页面上显示警报消息,那么就我所知,使用Ajax或XMLHttpRequest是唯一的方法。 –

相关问题