You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

用户角色管理页重置权限后window.location.reload()未生效如何解决

你的问题核心是fetch是异步操作,你把window.location.reload()放在了fetch逻辑的外层,会在请求刚发出去、还没收到后端响应的时候就直接触发页面刷新,等于打断了未完成的请求流程,所以看起来像执行到reload前就停止了。另外你当前的getPermessi()函数只是读取页面上现有表单的取值,没有主动从后端拉取重置后的最新权限数据,就算执行了也不会更新页面展示的权限信息。

你可以根据需求选择以下两种修复方案:

方案1:等重置请求完成后再刷新页面(不用改getPermessi)

把window.location.reload()移到fetch的成功回调里,确保接口返回成功后再刷新页面,代码修改如下:

let ResetUserPermission = document.getElementById("btn-reset-ruoli");
if (ResetUserPermission)
    ResetUserPermission.addEventListener("click", async ev => {
        ev.preventDefault();

        SwalWrapper.fire({
            type: "question",
            title: "Do you want to restore the default values?",
            showCancelButton: true

        }).promise.then(async result => {
            if (result) {
                console.log("You clicked OK");
                let menu = [];
                let tab = [];
                let func = [];

                var val = $("#user").val();
                $(".combomenu").each(function () {
                    menu.push($(this).val());
                })
                $(".combotab").each(function () {
                    tab.push($(this).val());
                })
                $(".combofunc").each(function () {
                    func.push($(this).val());
                })
                console.log("Acquired Values")
                const data = {
                    IdUser: val,
                    IdMenu: menu,
                    IdTab: tab,
                    IdFunzione: func
                };

                try {
                    // 用await等待fetch请求完成
                    const response = await Core.compatibility.fetch("/RolesManagement/ResetUserPermission", {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/json',
                        },
                        body: JSON.stringify(data)
                    });
                    if (!response.ok) {
                        throw new Error(response.statusText);
                    } 
                    SwalWrapper.fire({
                        type: "success",
                        title: "Reset done"
                    });
                    // 确保请求成功后再刷新页面
                    window.location.reload();
                    console.log("Page reloaded");
                } catch (e) {
                    SwalWrapper.fire({
                        type: "error",
                        title: "Errore",
                        text: e
                    });
                };
            }
        })
    });

方案2:不刷新页面,修改getPermessi拉取最新数据局部更新

如果你不想整页刷新,可以改造getPermessi函数,让它主动请求后端接口拉取对应用户的最新权限,再把新值赋值到页面对应的选择框/展示区域,这样不用刷新也能看到最新结果。
你当前的getPermessi只有读取当前页面值的逻辑,需要补充请求后端、渲染页面的部分,示例逻辑如下:

export async function getPermessi() {
    const userId = $("#user").val();
    // 调用后端接口拉取最新权限,接口地址根据你实际后端路由调整
    const res = await fetch(`/RolesManagement/GetUserPermission?userId=${userId}`);
    const permessi = await res.json();
    // 把拿到的最新权限赋值到页面对应控件
    $(".combomenu").each((index, el) => {
        $(el).val(permessi.menu[index]);
    })
    $(".combotab").each((index, el) => {
        $(el).val(permessi.tab[index]);
    })
    $(".combofunc").each((index, el) => {
        $(el).val(permessi.func[index]);
    })
}

修改完成后,在fetch重置接口的成功回调里调用getPermessi()即可,不需要再执行window.location.reload()。

内容的提问来源于stack exchange,提问作者Matteo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.07 01:27:02