本文介绍了ajax调用中window.onbeforeunload出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 在我的应用程序中,我有一个图表可以保存。如果有人填写图表并且没有保存值,则应该弹出一个弹出窗口向他/她显示数据尚未保存。 我这样做... 在 窗口.onbeforeunload 我正在检查值字段的ID(存储在隐藏字段中)是否保存在数据库中。并且据我所知,我正在显示请假页面消息。 < script> window .onbeforeunload = function (evt){ / * 首先获取方案ID。 * / var id1 = document .getElementById( <%= hdScenario1.ClientID%>)。value; var id_1 = ' '; $ .ajax({ type: POST , url: scenarioAdd.aspx / CheckUpdateScenario, data:' {scenarioId:' + id1 + ' }', contentType: application / json; charset = utf-8, dataType: json ,成功: function (响应){ id_1 = response.d; if (id_1 == 0 ){ var message = ' 在关闭之前保存它。'; if ( typeof evt == ' undefined'){ evt = window .event; } if (evt){ evt.returnValue = message; } return 消息; } },失败: function (响应){ alert(response.d + f); id_1 = 0; },错误: function (响应){ alert(response.d + e); id_1 = 0; } }); // 保留页面代码在此之前。 } < / script> 主线 var message =&#39;在关闭前保存。&#39 ;; 如果( typeof evt ==&#39; undefined&# 39;){ evt = window .event; } if (evt){ evt.returnValue = message; } 返回消息; 是写在上面的评论部分。然后它运行正常。但这部分是在ajax之前执行的。所以最终的结果是空的。然后我把代码放入ajax成功。在此之后它无法正常工作。 请检查问题,如果可能的话修复它。 谢谢.. 解决方案 .ajax({ type: POST, url: scenarioAdd.aspx / CheckUpdateScenario, data:' {scenarioId:' + id1 + ' }', contentType: application / json; charset = utf-8, dataType: json,成功: function (响应){ id_1 = response.d; if (id_1 == 0 ){ var message = ' 在关闭之前保存它。'; if ( typeof evt == ' undefined'){ evt = window .event; } if (evt){ evt.returnValue = message; } return 消息; } },失败: function (响应){ alert(response.d + f); id_1 = 0; },错误: function (响应){ alert(response.d + e); id_1 = 0; } }); // 保留页面代码在此之前。 } < / script> 主线 var message =&#39;在关闭前保存。&#39 ;; 如果( typeof evt ==&#39; undefined&# 39;){ evt = window .event; } if (evt){ evt.returnValue = message; } 返回消息; 是写在上面的评论部分。然后它运行正常。但这部分是在ajax之前执行的。所以最终的结果是空的。然后我把代码放入ajax成功。在此之后它无法正常工作。 请检查问题,如果可能的话修复它。 谢谢.. 问题是您正在向服务器发出异步调用。在AJAX调用完成之前, beforeunload 事件处理程序返回,浏览器继续卸载页面。 根据文档 [ ^ ],您可以在传递给 async:false / blockquote> .ajax(...),这可能解决问题: window .onbeforeunload = function (evt){ / * 首先获取方案ID。 * / var id1 = document .getElementById( <%= hdScenario1.ClientID%>)。value; var id_1 = ' ' Hi,In my application I have a chart to save. If some one fill the chart and didn't save the values it should produce a pop up to show him/her that data has not been saved. I did it like...On window.onbeforeunload I am checking the ID of value field(which is stored in a hidden field) is saved or not from database. And according to that I am showing the leave page message or not.<script> window.onbeforeunload = function (evt) { /* get the scenario id first. */ var id1 = document.getElementById("<%= hdScenario1.ClientID %>").value; var id_1=''; $.ajax({ type: "POST", url: "scenarioAdd.aspx/CheckUpdateScenario", data: '{scenarioId: ' + id1 + '}', contentType: "application/json; charset=utf-8", dataType: "json", success: function (response) { id_1 = response.d; if (id_1 == 0) { var message = 'Save it before you close.'; if (typeof evt == 'undefined') { evt = window.event; } if (evt) { evt.returnValue = message; } return message; } }, failure: function (response) { alert(response.d + " f"); id_1 = "0"; }, error: function (response) { alert(response.d + " e"); id_1 = "0"; } }); // leave page code was before here. } </script>The main linesvar message = &#39;Save it before you close.&#39;;if (typeof evt == &#39;undefined&#39;) { evt = window.event;}if (evt) { evt.returnValue = message;}return message;was written on the comment section above. Then it was running ok. but this part is executing before ajax. so the ultimate result is null. Then I put the code into ajax success. And after this its not working. Kindly check the issue and if possible fixed it.Thanks.. 解决方案 .ajax({ type: "POST", url: "scenarioAdd.aspx/CheckUpdateScenario", data: '{scenarioId: ' + id1 + '}', contentType: "application/json; charset=utf-8", dataType: "json", success: function (response) { id_1 = response.d; if (id_1 == 0) { var message = 'Save it before you close.'; if (typeof evt == 'undefined') { evt = window.event; } if (evt) { evt.returnValue = message; } return message; } }, failure: function (response) { alert(response.d + " f"); id_1 = "0"; }, error: function (response) { alert(response.d + " e"); id_1 = "0"; } }); // leave page code was before here. } </script>The main linesvar message = &#39;Save it before you close.&#39;;if (typeof evt == &#39;undefined&#39;) { evt = window.event;}if (evt) { evt.returnValue = message;}return message;was written on the comment section above. Then it was running ok. but this part is executing before ajax. so the ultimate result is null. Then I put the code into ajax success. And after this its not working. Kindly check the issue and if possible fixed it.Thanks..The problem is that you're issuing an asynchronous call to the server. The beforeunload event handler returns before the AJAX call has completed, and the browser continues with unloading the page.According to the documentation[^], you can specify async:false in the settings passed to .ajax(...), which might solve the problem:window.onbeforeunload = function (evt) { /* get the scenario id first. */ var id1 = document.getElementById("<%= hdScenario1.ClientID %>").value; var id_1 = ''; 这篇关于ajax调用中window.onbeforeunload出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-26 01:39