在readystate 0,status 0和statusText错误上跟踪AJAX错误 [英] Tracking AJAX error on readystate 0, status 0 and statusText error

查看:376
本文介绍了在readystate 0,status 0和statusText错误上跟踪AJAX错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读了所有这些类似的问题: 1 2 3 4 5 6 。但是我需要调试更多。

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

所有这些问题都表明问题是由于跨域策略和 e.preventDefault()解决问题。但我怀疑这可以打破其他一些事情,所以我需要确定。

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.

在我的网站上,我的操作每天被解雇15k次。当用户访问网页时,我的javascript文件会检查一个元素。如果元素存在,则进行AJAX调用。

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.

$(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
});

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

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

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

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 ?

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

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

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

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

Edit2:此页说明:


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

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时单击某个链接来重现该问题。

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

推荐答案

已解决。

我遇到了同样的问题。在ajax调用后有重定向时会出现此问题。

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

代码损坏:

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

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

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

In the above code, there is redirect after the ajax which gives error of ready state 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";
                }
            });

这篇关于在readystate 0,status 0和statusText错误上跟踪AJAX错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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