javascript - How to fix? Chrome console : ERR_UNSAFE_REDIRECT - Stack Overflow

Post request webserver to the nodejs backend:$('#login').click(function() {$.post('logi

Post request webserver to the nodejs backend:


     $('#login').click(function() {
        $.post(
            '/login',
            {
                mail: encodeURIComponent($('#mail').val()),
                password: encodeURIComponent($('#password').val())
            },
            function(result) {
                console.log(result);
            });
    });



Problem


The backend get successfull the post request, but the redirect to the new page will not happen cause I´ve in the chrome console this error:

POST http://localhost:1000/login net::ERR_UNSAFE_REDIRECT

I know this is not a protected SSL connection. But is there a way to fix it? Or did I made some settings wrong?



Code I´ve in the backend:


app.post('/login', function(req, res) {
   console.log(req.body);
   res.redirect(__dirname+'/wwwroot/views/profile/dashboard.html');
});

Post request webserver to the nodejs backend:


     $('#login').click(function() {
        $.post(
            '/login',
            {
                mail: encodeURIComponent($('#mail').val()),
                password: encodeURIComponent($('#password').val())
            },
            function(result) {
                console.log(result);
            });
    });



Problem


The backend get successfull the post request, but the redirect to the new page will not happen cause I´ve in the chrome console this error:

POST http://localhost:1000/login net::ERR_UNSAFE_REDIRECT

I know this is not a protected SSL connection. But is there a way to fix it? Or did I made some settings wrong?



Code I´ve in the backend:


app.post('/login', function(req, res) {
   console.log(req.body);
   res.redirect(__dirname+'/wwwroot/views/profile/dashboard.html');
});
Share Improve this question edited Jan 22, 2018 at 13:11 AA Shakil 5564 silver badges16 bronze badges asked Jan 22, 2018 at 12:56 Lukas GundLukas Gund 7112 gold badges10 silver badges30 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 2

You are sending a redirect response to ajax request, why not just let javascript do the redirect for you by using window.location.href:

Server:

app.post('/login', function(req, res) {
    console.log(req.body);
    res.json({ success: true });
});

Client:

$('#login').click(function() {
    $.post(
        '/login',
        {
            mail: encodeURIComponent($('#mail').val()),
            password: encodeURIComponent($('#password').val())
        },
        function(result) {
            if (result.success) {
                window.location.href = '/dashboard'
            }
        });
});

Note that you should define /dashboard route if you want the example above works:

app.get('/dashboard', function(req, res) {
    ...
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745651778a4638306.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信