javascript 在 readystate 0、status 0 和 statusText 错误上跟踪 AJAX 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25025465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 03:46:29  来源:igfitidea点击:

Tracking AJAX error on readystate 0, status 0 and statusText error

javascriptjqueryajax

提问by trante

I read all these similar questions: 1, 2, 3, 4, 5and 6. But I need to debug more.

我阅读了所有这些类似的问题:123456。但我需要调试更多。

All of that questions say that problem is due to cross domain policy and e.preventDefault()solves problem. But I doubt that this can break some other things so I need to be sure.

所有这些问题都说问题是由于跨域策略并e.preventDefault()解决了问题。但我怀疑这会破坏其他一些东西,所以我需要确定。

In my site I have an operation which is fired 15k times a day. When user visits a webpage, my javascript file checks for an element. If element exists it makes an AJAX call.

在我的网站中,我有一个每天被触发 15,000 次的操作。当用户访问网页时,我的 javascript 文件会检查元素。如果元素存在,它将进行 AJAX 调用。

$(document).ready(function() {

    // I do something here

    if ($('#mydiv').length !== 0) {

       var startTime = new Date().getTime();        

       $.ajax({
           type: "POST",
           url: "/mypage",
           success: function (data) {
               // I do something here
           },
           timeout: 20000,
           error: function(r, s, e) {
               var elapsedTime = (new Date().getTime() - startTime );
               var string = "readyState: "+r.readyState+", status: "+r.status+", statusText: "+r.statusText+", responseText: "+r.responseText+", textStatus: "+s+", error: "+e +", elapsedTime: "+elapsedTime;
               // send this string to server for logging
              formData = {string:string};
              $.ajax({
                url : "/mylog",
                type: "POST",
                data : formData
              });
           }
       });
     }

    // I do something here
});

For the 15k requests, ~70 times user gets AJAX error. ~20 is due to timeout. ~50 is due to an unknown error. When I check clients' logs I see that AJAX request gives these values for unknown error: readyState: 0, status: 0 and statusText: error

对于 15k 请求,大约 70 次用户收到 AJAX 错误。~20 是由于超时。~50 是由于未知错误。当我检查客户端的日志时,我看到 AJAX 请求为未知错误提供了这些值:readyState: 0, status: 0 and statusText: error

Although e.preventDefault() is recommended for this issue. I need to know what user makes for this error. In my javascript file I don't make any cross domain request. But I can't know what user made for this error ? Is it possible to know more for this error ?

尽管针对此问题建议使用 e.preventDefault()。我需要知道用户对此错误的原因。在我的 javascript 文件中,我没有提出任何跨域请求。但我不知道用户为此错误做了什么?是否可以了解更多有关此错误的信息?

Note: I track the time between "AJAX call start" and "AJAX error". Average time is 1500-2000 ms.

注意:我跟踪“AJAX 调用开始”和“AJAX 错误”之间的时间。平均时间为 1500-2000 毫秒。

Edit: If I use preventDefault(), where can I put it ? Because I don't register a click function etc.

编辑:如果我使用preventDefault(),我可以把它放在哪里?因为我没有注册点击功能等。

Edit2: This pagesays this:

Edit2:这个页面是这样说的:

We found out that this could happen if the ajax request is getting canceled before it completes. We could reproduce the problem if we triggered the ajax request and then immediately click on a link to navigate away from the page. jQuery throws the error event when the user navigates away from the page either by refreshing, clicking a link, or changing the URL in the browser.

我们发现如果 ajax 请求在完成之前被取消,就会发生这种情况。如果我们触发 ajax 请求,然后立即单击链接离开页面,我们可以重现该问题。当用户通过刷新、单击链接或更改浏览器中的 URL 离开页面时,jQuery 会抛出错误事件。

But I couldn't reproduce the problem by clicking some link while AJAX is loading.

但是我无法通过在加载 AJAX 时单击某个链接来重现该问题。

回答by Bhavin Visariya

Solved.

解决了。

I was facing the same issue. This issue occurs when there is redirect after the ajax call.

我面临同样的问题。在 ajax 调用后重定向时会出现此问题。

Broken Code :

损坏的代码:

$.ajax({
                url: '',
                dataType: 'jsonp',
                success: function (json) {
                    // do stuff here
                },
                error: function (error) {
                    // do stuff here
                }
            });

 window.location.href = "www.google.com";

In the above code, there is redirect after the ajax which gives error of ready state 0.

在上面的代码中,ajax 之后有重定向,它给出了就绪状态 0 的错误。

Solution :Write redirect after getting some response. I wrote within complete.

解决方案:得到一些响应后写重定向。我写的很完整。

$.ajax({
                url: '',
                dataType: 'jsonp',
                success: function (json) {
                    // do stuff here
                },
                error: function (error) {
                    // do stuff here
                },
                complete: function () {
                    window.location.href = "www.google.com";
                }
            });