jQuery的$当请求失败,因为浏览器重装阿贾克斯调用成功处理程序 [英] jQuery $.ajax calls success handler when request fails because of browser reloading

查看:184
本文介绍了jQuery的$当请求失败,因为浏览器重装阿贾克斯调用成功处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code:

 $.ajax({
          type: "POST",
          url: url,
          data: sendable,
          dataType: "json",
          success: function(data) {

               if(customprocessfunc)
                   customprocessfunc(data);
         },

          error: function(XMLHttpRequest, textStatus, errorThrown){
                    // error handler here
                }
        });

我有一个计时器,这使得AJAX请求频繁。如果我没有收到在数据什么,我显示错误信息给用户 - 这意味着,东西坏了在服务器上

I have a timer which makes AJAX requests often. If I do not receive anything in 'data', I show an error message to the user - it means, something went bad on the server.

现在的问题是,当用户重新加载页面,而AJAX调用正在进行中。我可以在萤火虫看到AJAX调用失败(网址为红色并显示没有HTTP状态),所以我希望jQuery将停止操作,或者至少去错误处理程序。但它关系到成功的​​处理程序,并通过空的数据的变量。

The problem is when user reloads the page while the AJAX call is in progress. I can see in the Firebug that the AJAX call fails (URL is colored red and no HTTP status is displayed) so I expect that jQuery will stop the request or at least go to the error handler. But it goes to the success handler and passes null in the 'data' variable.

这样一来,当用户重新加载页面,有时他能看到我的大红色的消息,关于未知错误(因为数据为空)。

As a result, when user reloads the page, sometimes he can see my big red message about unknown error (because data is null).

有没有什么办法让jQuery的中止在完全重装请求或至少不打电话给我成功的功能?我没有办法知道在成功处理程序为什么数据为空 - 这样做是来自服务器空或呼叫被放弃,因为重新加载。

Is there any way to make jQuery abort the request on complete reloading or at least not to call my success function? I have no way to know in the success handler why the data is null - did it came empty from the server or the call was aborted because of reload.

推荐答案

您可以使用 onbeforeunload的活动页面上:它是清除所有的悠逸AJAX链接功能请求。

You can use the onbeforeunload event on your page: link it to a function that cleans all youe AJAX requests.

这篇关于jQuery的$当请求失败,因为浏览器重装阿贾克斯调用成功处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆