如何将Express.js的响应打印到POST请求html页面?

时间:2015-04-10 08:39:01

标签: javascript jquery html express

我想在登录页面上打印登录验证消息。但是响应正在打印在一个新的空白页面上。有没有办法获取响应并打印任何现有的div标签?

我使用Express.js来处理登录验证。

app.post('/login',function(req,res){
    var user_name=req.body.user;
    var password=req.body.pwd;

    console.log("User name = "+user_name+", password is "+password);

    if (user_name=="sudheesh" && password=="pass") {
        sess = req.session;
        sess.user=user_name;
        console.log("Logged in..");
        res.send('done');
    }
    else {
        res.send('not_done');
    }
});

HTML脚本如下所示,

<script>
    $(document).ready(function(){
        var user,pwd;
        $("#submit").click(function(){
            email=$("#user").val();
            pass=$("#pwd").val();
            /*
            * Perform some validation here.
            */
            $.post("http://localhost:3000/login",{user:user,pwd:pwd},function(data){ 
              console.log(data);
        if(data=='done')           
        {
            alert("you have logged in..!");
            window.location.href="/about";
        }
    });
        });
    });
</script>

0 个答案:

没有答案