admin管理员组

文章数量:1026989

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

本文标签: javascriptHow to fix Chrome consoleERRUNSAFEREDIRECTStack Overflow