最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin6PV0评论

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) {
    ...
});
Post a comment

comment list (0)

  1. No comments so far