首页 > 解决方案 > 注销后重定向用户

问题描述

我正在尝试制作一个注销系统,在 c# 中注销正常工作,但是在前端,当我单击注销按钮时,isLogged 变量在数据库中更改为 0 of 1(应该是)但在浏览器中用户未重定向到登录页面,我收到错误“请求的 JSON 解析失败”

我尝试让 ajax 调用正常工作,我在这个站点上搜索解决方案,但我无法解决这个问题

 var userAIDI = window.localStorage.getItem('userId');
    console.log(userAIDI)
    var usr = {
        userId:userAIDI
    }
$.ajax({
        type: "POST",
        url: "https://localhost:44378/api/userLogOut",
        data: JSON.stringify(usr),
        contentType: "application/json",
        dataType: "json",
        success: function (data) {
            window.location.href = "pages/login.html";


            // Replace the div's content with the page method's return.

        },
        error: function (jqXHR, exception) {
            var msg = '';
            if (jqXHR.status === 0) {
                msg = 'Not connect.\n Verify Network.';
            } else if (jqXHR.status == 404) {
                msg = 'Requested page not found. [404]';
            } else if (jqXHR.status == 500) {
                msg = 'Internal Server Error [500].';
            } else if (exception === 'parsererror') {
                msg = 'Requested JSON parse failed.';
            } else if (exception === 'timeout') {
                msg = 'Time out error.';
            } else if (exception === 'abort') {
                msg = 'Ajax request aborted.';
            } else {
                msg = 'Uncaught Error.\n' + jqXHR.responseText;
            }
            alert(msg);
        },
    });

标签: c#asp.net-web-api

解决方案


问题可能与 .html 扩展名有关。

浏览器倾向于在浏览历史记录中保留 .html 的缓存更长时间。尝试在谷歌浏览器的隐身标签中测试该功能,或尝试将所有文​​件转换为 .asp 或 .php(无论您使用的是哪个)。

我建议这样做是因为您的代码看起来不错,并且数据库也可以正确反映,正如您在此处所建议的那样:

“ isLogged 变量在数据库中更改为 0 of 1(应该如此),但在浏览器中,用户不会被重定向到登录页面”。


推荐阅读