简体   繁体   English

怎么修? Chrome控制台:ERR_UNSAFE_REDIRECT

[英]How to fix? Chrome console : ERR_UNSAFE_REDIRECT

Post request webserver to the nodejs backend: 将请求Web服务器发布到nodejs后端:


     $('#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: 后端成功获得了发布请求,但由于我在chrome控制台中出现了以下错误,因此无法重定向到新页面:

POST http://localhost:1000/login net::ERR_UNSAFE_REDIRECT POST http:// localhost:1000 /登录网:: ERR_UNSAFE_REDIRECT

I know this is not a protected SSL connection. 我知道这不是受保护的SSL连接。 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'); }); 

You are sending a redirect response to ajax request, why not just let javascript do the redirect for you by using window.location.href : 您正在发送对ajax请求的重定向响应,为什么不让javascript通过使用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: 请注意,如果希望上面的示例起作用,则应定义/dashboard路线:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何避免 Chrome 控制台错误 Unsafe JavaScript? - How to avoid Chrome Console error Unsafe JavaScript? 如何在Chrome控制台的此代码中解决此错误? - How to fix this error in this code for chrome's console? 如何修复 err_http_headers_sent - How to fix err_http_headers_sent 如何修复控制台中的“UnhandledPromiseRejectionWarning” - How to fix 'UnhandledPromiseRejectionWarning' in console 如何在 web 页面而不是 chrome 扩展页面中运行不安全内联 - how to run unsafe-inline in a web page, not in chrome extension page 如何在Chrome中获得此控制台? - How to get this console in Chrome? 如何解决此重定向循环? - How to fix this redirect loop? 如何处理 catch(err) 块上的 @typescript-eslint/no-unsafe-member-access 规则? - How to handle the @typescript-eslint/no-unsafe-member-access rule on catch(err) blocks? 为什么多次Unicode转换String.fromCharCode(“👉” .charCodeAt(0))破坏了Chrome控制台中的符号,以及如何解决该符号? - Why multiple unicode conversions String.fromCharCode(“👉”.charCodeAt(0)) ruin the symbol in Chrome console and how to fix it? vscode chrome 开发者工具控制台错误出现在第 2586 行,我该如何解决? - vscode chrome developer tools console error on line 2586 how can i fix this?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM