window.onbeforeunload() 事件调用ajax的解决方法

 1 function window.onbeforeunload() {
 2 
 3     var jhid = $("#ctl00_ContentBody_hfGuid").val();
 4     $.ajax({
 5         url: "AjaxServices/AjaxService.asmx/DeleteDeviceAndWorkContent", // ajax 调用后台方法
 6         type: "POST",
 7         async: false,
 8         data: "{'jhid':'" + jhid + "'}", // 参数
 9         dataType: "json", // 返回类型
10         contentType: "application/json; charset=utf-8",
11         //成功时调用的方法
12         success: function(data) {
13         },
14         error: function(XMLHttpRequest, textStatus, errorThrown) {
15             alert(XMLHttpRequest);
16             alert(textStatus);
17             alert(errorThrown);
18         }
19     });
20     
21 }


解决方法:

使用ajax,设置为同步,不要使用异步(上面代码红色显示!)

原文地址:https://www.cnblogs.com/zhangwei595806165/p/2695362.html