什么是明确的方法来调用jQuery的AJAX功能的窗口卸载过吗?方法来、明确、窗口、功能

2023-09-11 01:22:55 作者:不要愁眉不展

我需要调用jQuery的阿贾克斯()函数的窗口卸载之前。在我的情况下,它是POST,我不关心返回结果,我只想一个数据库进行更新。

I need to call the jquery ajax() function before a window unloads. In my case its a POST and I dont care about the return result, I just want a database to be updated.

因此​​,答案似乎是一个函数绑定到卸载事件,即使用jQuery的卸载()函数,该函数使一个AJAX调用。另外,我知道你们都应该同步参数添加到通话。换句话说,增加异步:假的了jQuery阿贾克斯()调用

So the answer seems to be to bind a function to the unload event, i.e. use the jquery unload() function, and that function makes an ajax call. In addition, I know you are supposed to add the synchronous parameter to the call. In other words, add async: false to the jquery ajax() call.

不过,我已经尝试了所有这一切,并没有对IE8的工作!我已经测试了很多次,它根本不会对IE8的工作。在Firefox和Chrome浏览器正常工作。

However, I have tried all this and it does not work on IE8! I have tested it many times and it simply will not work on IE8. Works fine in Firefox and Chrome.

更奇怪的是,在我关闭了整个IE8的应用程序,它好像那么调用经过!难道是被缓存?

Even stranger is that after I close the whole IE8 application, it seems as if then the call goes through! Is it being cached?

所以,其他人都建议要绑定的AJAX使用jQuery绑定beforeunload()。不过似乎有在这是否会在所有的浏览器的一些混合的答案。

So other people are suggesting to bind the ajax beforeunload using jquery bind(). However there seems to be some mixed answers on whether this will work on all browsers.

我还没有尝试过的beforeunload,因为这似乎是做它的黑客的方式。但是,如果它的工作原理,然后我真的不只要它的工作在意。

I have not yet tried the beforeunload as this seems like the "hack" way of doing it. However, if it works then I dont really care as long as it does work.

推荐答案

你能不能调用函数在页面的body标签?

Can you not call the function in the body tag of the page?

<body onbeforeunload="funcName(param)">

这样每次页面卸载它时调用的函数,你不担心绑定。

that way every time the page unloads it calls your function and you don't have to worry about binding.