简体   繁体   English

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

[英]Tracking AJAX error on readystate 0, status 0 and statusText error

I read all these similar questions: 1 , 2 , 3 , 4 , 5 and 6 . 我读完所有这些类似的问题: 123456 But I need to debug more. 但我需要调试更多。

All of that questions say that problem is due to cross domain policy and e.preventDefault() solves problem. 所有这些问题都表明问题是由跨域策略引起的, e.preventDefault()解决了问题。 But I doubt that this can break some other things so I need to be sure. 但我怀疑这可以打破其他一些事情,所以我需要确定。

In my site I have an operation which is fired 15k times a day. 在我的网站上,我有一个每天被发射15k次的操作。 When user visits a webpage, my javascript file checks for an element. 当用户访问网页时,我的javascript文件会检查一个元素。 If element exists it makes an AJAX call. 如果元素存在,则进行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. 对于15k请求,~70次用户获得AJAX错误。 ~20 is due to timeout. ~20是因为超时。 ~50 is due to an unknown error. ~50是由于未知错误。 When I check clients' logs I see that AJAX request gives these values for unknown error: readyState: 0, status: 0 and statusText: error 当我检查客户端的日志时,我看到AJAX请求为未知错误提供了这些值: readyState: 0, status: 0 and statusText: error

Although e.preventDefault() is recommended for this issue. 虽然建议针对此问题使用e.preventDefault()。 I need to know what user makes for this error. 我需要知道用户为此错误做了什么。 In my javascript file I don't make any cross domain request. 在我的javascript文件中,我没有提出任何跨域请求。 But I can't know what user made for this error ? 但我不知道用户为此错误做了什么? Is it possible to know more for this error ? 是否可以了解更多此错误?

Note: I track the time between "AJAX call start" and "AJAX error". 注意:我跟踪“AJAX调用开始”和“AJAX错误”之间的时间。 Average time is 1500-2000 ms. 平均时间为1500-2000毫秒。

Edit: If I use preventDefault() , where can I put it ? 编辑:如果我使用preventDefault() ,我可以把它放在哪里? Because I don't register a click function etc. 因为我没有注册点击功能等。

Edit2: This page says this: 编辑2: 这个页面说明了这个:

We found out that this could happen if the ajax request is getting canceled before it completes. 我们发现,如果ajax请求在完成之前被取消,则可能会发生这种情况。 We could reproduce the problem if we triggered the ajax request and then immediately click on a link to navigate away from the page. 如果我们触发了ajax请求,然后立即点击链接导航离开页面,我们就可以重现问题。 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. 当用户通过刷新,单击链接或更改浏览器中的URL导航离开页面时,jQuery会抛出错误事件。

But I couldn't reproduce the problem by clicking some link while AJAX is loading. 但是我无法通过在加载AJAX时单击某个链接来重现该问题。

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";
                }
            });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 {"readyState":0",status:"0",statusText":error"} - {"readyState":0",status:"0",statusText":error"} AJAX发布-{“ readyState”:0,“ responseText”:“”,“ status”:0,“ statusText”:“ error”} - AJAX post - {“readyState”:0,“responseText”:“”,“status”:0,“statusText”:“error”} jquery ajax readystate 0 responsetext status 0 statustext error - jquery ajax readystate 0 responsetext status 0 statustext error jQuery ajax响应“ readyState”:0,“状态”:0,“ statusText”:“错误” - jquery ajax response “readyState”:0,“status”:0,“statusText”:“error” jQuery Ajax错误{“ readyState”:0,“ responseText”:“”,“ status”:0,“ statusText”:“ OK”} - JQuery Ajax Error {“readyState”:0,“responseText”:“”,“status”:0,“statusText”:“OK”} jquery ajax调用使用readystate 4,状态200,statustext ok返回错误 - jquery ajax call returning an error with readystate 4, status 200, statustext ok FCM 响应 {"readystate":0,"responsetext":"","status":0,"statustext":"error"} - FCM responds {"readystate":0,"responsetext":"","status":0,"statustext":"error"} 错误回调{{readyState“:4,”status“:200,”statusText“:”success“} - error callback with {“readyState”:4,“status”:200,“statusText”:“success”} 骨干获取:{“readyState”:0,“responseText”:“”,“status”:0,“statusText”:“error”} 仅在移动设备上 - backbone fetch: {“readyState”:0,“responseText”:“”,“status”:0,“statusText”:“error”} only on mobile AJAX获取请求JSONP分析器错误:readyState“:4,” status”:200,“ statusText”:“ load”}] - AJAX get request JSONP parsererror: readyState“:4,”status“:200,”statusText“:”load"}]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM