Nodejs重定向不起作用

时间:2016-01-05 07:00:22

标签: jquery node.js forms express url-routing

我是节点新手。无论我想要做什么都是简单的从提交和提交后重定向到特定的模板。这是我的目录结构

  • node_modules
  • index.js
  • 的login.html
  • 的test.html

这是我的index.js文件

var app = require('express')();
var http = require('http').Server(app);
var express = require('express')
var bodyParser = require('body-parser')

app.get('/', function(req, res) {
    res.sendFile(__dirname + '/login.html');

});

app.post('/login', function(request, response) {
    console.log(request.body.user);
    console.log(request.body.password);
    if (request.body.user === "test@gmail.com" && request.body.password === "123") {
        console.log("logged in");
        response.statusCode = 200;
        response.setHeader("Location", "/home");
        response.end();
    }
});

app.get('/home', function(req, res) {
    res.sendFile(__dirname + '/test.html');

});

http.listen(3001, function() {
    console.log('listening on *:3001');
});

在我的login.html文件中,我有一个简单的表单

        <label for="user" class="sr-only">Email address</label>
        <input type="email" id="user" class="form-control" name="user[email]" placeholder="Email address" required autofocus autocomplete="on">
        <label for="password" class="sr-only">Password</label>
        <input type="password" id="password" name="user[password]" class="form-control" placeholder="Password" required>
        <div class="checkbox">
            <label>
                <input type="checkbox" value="remember-me"> Remember me
            </label>
        </div>
        <button class="btn btn-lg btn-primary btn-block" type="button" id="submit">Sign in</button>

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script>
        $(document).ready(function() {
            var user, pass;
            $("#submit").click(function(event) {
                user = $("#user").val();
                pass = $("#password").val();
                $.post("/login", {user: user, password: pass}, function(data) {
                    if (data === 'done')
                    {
                        alert("login success");
                    }
                });
            });
        });
    </script>

当我在localhost:3001 login.html处运行我的应用时,如果我提交的表单包含username:test@gamil.compassword:123,则不会重定向到test.html,虽然我可以阅读控制台消息"logged in"

2 个答案:

答案 0 :(得分:0)

你能尝试使用

吗?
app.post('/login', function(request, response) {
    console.log(request.body.user);
    console.log(request.body.password);
    if (request.body.user === "test@gmail.com" && request.body.password === "123") {
        console.log("logged in");
        response.redirect('/home')
    }
});

有关快速重定向的文档(http://expressjs.com/en/api.html#res.redirect

答案 1 :(得分:0)

不要通过AJAX调用(if (!pref.getSectionStatus1()) { Log.e("Once Again in TableScreenSectionwiseActivity: ", "hmm...."); GetSectionsAsync getSectionsAsync = new GetSectionsAsync( getActivity()); getSectionsAsync.setFragmentInterfaceCallBack(this); HashMap<String, String> map = new HashMap<String, String>(); map.put("Location_Name", pref.getLocation()); getSectionsAsync.execute(map); })向@Override public void onTaskCompleted(HashMap<String, ArrayList<String>> hashMap) { ids = hashMap.get("ID_LIST"); names = hashMap.get("NAME_LIST"); table_section_adapter adapter = new table_section_adapter( activity.getApplicationContext(), names, ids); section_list.setAdapter(adapter); section_list.setOnItemClickListener(this); }发出请求,而是让表单提交给它:

/login

在您目前的情况下,您将 AJAX请求重定向到$.post(),而不是整个页面。

此外,当用户名和/或密码不匹配时,您的处理程序不执行任何操作,这意味着请求将停止。在这种情况下,您还需要发送回复(可能会重定向到错误页面,或者再次返回到登录页面)。

相关问题