求解答啊,window.location.href=页面跳转不生效,

window.location.href,页面跳转没有生效,呜


                        //发送POST异步请求
                        $.post("registerServlet",$(this).serialize(),function (data) {
                            //处理服务器返回的数据data,{flag,true,errorMsg:"注册失败"}'
                            if (data.flag){
                                //成功注册
                                alert("chenggong");
                                //跳转页面,这一行代码没有生效,很奇怪
                                window.location.href="http://localhost:8080/travel/register_ok.html";
                                //这一行就可以正确执行
                                document.write(location.href);
                            }else {
                                $("#errorMsg").html(data.errorMsg);
                            }
                        });

去掉document.write(location.href);这句,由于是异步的,文档流已经关闭,document.write会覆盖掉当前的所有内容包含js脚本,有些浏览器可能做了优化,导致没跳转。chrome 92和Firefox91测试都无法跳转

去掉了document.write和alert语句只保留location.href还不能跳转?setTimeout延时跳转只在几年前旧版本浏览器上出现这种解决办法,新版本暂时没发现你这种问题,题主什么浏览器?

有帮助或启发麻烦点个采纳【本回答右上角】,谢谢~~有其他问题可以继续交流~

我之前可以是没用异步可以跳转,应该在异步函数里你这个跳转会被document操作影响,你不用设置延迟,去掉document.write或者把跳转放在write之后就行

img

试试

top.location = 'https://web.com';

兄弟们,我解决了,延迟跳转页面就可以了,我给页面跳转延迟了半秒的时间,不过我不明白为什么要延迟跳转,不知道有没有人懂得,.......


                        //发送POST异步请求
                        $.post("registerServlet",$(this).serialize(),function (data) {
                            //处理服务器返回的数据data,{flag,true,errorMsg:"注册失败"}'
                            if (data.flag){
                                //成功注册
                                alert("chenggong");
                                //跳转页面,这一行代码没有生效,很奇怪
                                //window.location.href="http://localhost:8080/travel/register_ok.html";
                                window.setTimeout("window.location='register_ok.html'",500);
                                //这一行就可以正确执行
                                //document.write(location.href);
                            }else {
                                $("#errorMsg").html(data.errorMsg);
                            }
                        });

你跳转的是本页面吗