当请求由于浏览器重新加载而失败时,jQuery $.ajax 调用成功处理程序

发布于 2024-10-09 13:14:29 字数 790 浏览 0 评论 0原文

我有以下代码:

 $.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 请求的计时器。如果我没有收到“数据”中的任何内容,我会向用户显示一条错误消息 - 这意味着服务器上出现了问题。

问题是当 AJAX 调用正在进行时用户重新加载页面。我可以在 Firebug 中看到 AJAX 调用失败(URL 为红色并且没有显示 HTTP 状态),因此我希望 jQuery 将停止请求或至少转到错误处理程序。但它会转到成功处理程序并在“data”变量中传递 null。

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

有没有办法让 jQuery 在完全重新加载时中止请求,或者至少不调用我的成功函数?我无法知道成功处理程序中为什么数据为空 - 它是否从服务器变为空,或者调用因重新加载而中止。

I have the following 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
                }
        });

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.

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).

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

悲喜皆因你 2024-10-16 13:14:29

您可以在页面上使用 onbeforeunload 事件:将其链接到清理所有 AJAX 请求的函数。

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

物价感观 2024-10-16 13:14:29

统计数据很奇怪,如果调用失败,您会收到“成功”。但是您可以尝试通过监听完成事件来覆盖该行为(这里只是从我的测试框架中复制粘贴代码)

$.ajax(
    {
        url: url,
        type: type,
        processData: false,
        contentType: 'application/json; charset=utf-8',
        data: JSON.stringify(data),
        dataType: 'json',
        complete: function (result) {
            if (result.status == 404) {
                ok(false, '404 error');
            } else {
                callback($.parseJSON(result.responseText));
            }
        }
    });

结果是 XMLHttpRequest 对象,所以您可以分析它的实际状态并决定继续或不继续。

Stats strange that you are recievesing 'success' in case of failed call.. But you can try to override the behaviour, by listeining to complete event (here just a copy-paste a code from my test framework)

$.ajax(
    {
        url: url,
        type: type,
        processData: false,
        contentType: 'application/json; charset=utf-8',
        data: JSON.stringify(data),
        dataType: 'json',
        complete: function (result) {
            if (result.status == 404) {
                ok(false, '404 error');
            } else {
                callback($.parseJSON(result.responseText));
            }
        }
    });

result is XMLHttpRequest object, so you can analyze actual status of it and decide to proceed or not proceed.

肩上的翅膀 2024-10-16 13:14:29

onbeforeunlaod 技术不适用于定期刷新页面(例如每半秒)。我发现可以通过延迟少量的错误处理过程来避免刷新页面引起的错误。

示例:

$.ajax(...)
.success(...)
.error(function(jqXHR) {
setTimeout(function() {
  // error showing process
}, 1000);
});

除此之外

window.onbeforeunload = function() {//停止ajax调用}

事件可用于不太频繁刷新的ajax调用。

The onbeforeunlaod technique does not work for a periodically refreshing page (for example every half seconds). I have figured out that the error caused by refreshing the page can be avoided using delaying the error handling process by a small amount of time.

Example:

$.ajax(...)
.success(...)
.error(function(jqXHR) {
setTimeout(function() {
  // error showing process
}, 1000);
});

In addition to that

window.onbeforeunload = function() {//stop ajax calls}

event can be used for less frequently refreshing ajax calls.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文