在node.js中重定向到不同的URL后检索表单值

Retrieve form value after redirecting to different URL in node.js

本文关键字:URL 检索 表单 js node 重定向      更新时间:2023-09-26

假设我有一个只要求username的表单,并且有一个名为login的按钮。当前场景的URL为localhost:8080/beforelogin。

一旦我点击login,表单重定向到localhost:8080/loginsuccess。我必须显示消息欢迎用户 (username是在localhost:8080/login之前输入的)。

我可以获取username,但由于页面刷新(重定向到不同的URL),我不能在不同的URL上显示它。

我的客户端代码

$('.login').on('click', function()     // login is class of button Login
    {
        document.location.href = "/loginsuccess";    
        var username=$('.username_entered').val());   // username_entered is class username entered before login.
        $('.user-name-to-publish').html(username);    // user-name-to-publish is the class where i have to publish username entered before login
    });

如果我不清楚的话,我可以解释更多。

这样做的一种方法是将输入的用户名作为url中的参数传递。

$('.login').on('click', function()     // login is class of button Login
    {
        var username=$('.username_entered').val());
        document.location.href = "/loginsuccess?username=" + username;
    });

,然后在loginsuccess页面中获取用户名。查看这个博客了解更多信息:

http://www.sitepoint.com/url-parameters-jquery/

另一种方法是将其发布到服务器,并让服务器将您的用户重定向到loginsuccess页面,然后让服务器发送用户名