登录表单验证,无需重新加载页面

时间:2019-09-28 15:15:27

标签: javascript node.js frontend

我有一个登录表单,我想在不重新加载页面的情况下进行确认。我怎样才能做到这一点? AJAX还是Express(NodeJS)?

<form class="login-form" method="POST" @submit.prevent>
<p>Login to the chat.</p>
<br>
<p>Login:</p> <input type="text">
<p>Password:</p> <input type="password">
</form>

1 个答案:

答案 0 :(得分:0)

您应该首先删除您的“ form”标签,然后添加一个按钮,以激活例如“ login”之类的功能

我建议使用axios:

在您的函数“登录”中写:

    axios.post('/loginpath', {
    name: 'Fred',
     password:  'Flintstone'
     })
    .then(function (response) {
     if(response == "ok")
     { 
      window.location.replace("http://website.com/redirect);
     })
     .catch(function (error) {
       console.log(error);
        });

在服务器端,您捕获到类似

的请求
    app.post("/loginpath", (req,res) => {
    res.send("ok")
    })

您将需要暂停“ bodyparser”以从您的身体获取数据。
安装bodyparser时,您可以使用req.body.name或req.body.password访问提交的数据