简体   繁体   English

用jQuery通过Ajax发送数据时处理异常的问题

[英]Problem with handling exception when sending data by ajax with jQuery

i got following part of one of functions 我得到了其中一项职能的一部分

        if(continiueSend)
        {
            $.ajax
            ({
                type: "POST",
                url: "mailer.php",
                data: "somestestdata",
                timeout: 5000,
                success: function(a) 
                {
                    alert(a);
                },
                error: function (xhr, ajaxOptions, thrownError)
                {
                    alert(xhr.status);
                    alert(thrownError);
                }    
            });
        }

And it works great when server is ok, requested data are sent in less than 100ms, in case when i turnoff sever it also works great,script reports errors as it expected, but there is problem when server is busy 当服务器正常时,它运行良好,请求的数据在不到100毫秒内发送,以防我关闭服务器时也运行良好,脚本报告了预期的错误,但服务器繁忙时出现问题

When time of sending data exceeds limit set with "timeout:" error handling as specified in "error:" isn't fired, and in console appears following information 当发送数据的时间超过使用“超时:”设置的限制时,不会触发“错误:”中指定的错误处理,并且在控制台中显示以下信息

uncaught exception: [Exception... "Component returned failure code: 0x80040111 (NS_ERROR_NOT_AVAILABLE) [nsIXMLHttpRequest.status]" nsresult: "0x80040111 (NS_ERROR_NOT_AVAILABLE)" location: "JS frame :: http://127.0.0.1/site/index.php?article=2 :: anonymous :: line 252" data: no] 未捕获的异常:[异常...“组件返回失败代码:0x80040111(NS_ERROR_NOT_AVAILABLE)[nsIXMLHttpRequest.status]” nsresult:“ 0x80040111(NS_ERROR_NOT_AVAILABLE)”“位置:” JS框架:: http://127.0.0.1/site/index .php?article = 2 ::匿名::行252“数据:否]

Line 252 is first line of mentioned code. 第252行是上述代码的第一行。 I tryied to put code inside IF brackets into try...catch but with no results. 我试图将代码放在IF括号内放入try ... catch,但没有结果。

How I can properly detect this exception to handle it the way i want? 我如何正确检测此异常以所需方式处理它?

MTH MTH

Are you sure the error callback isn't actually called? 您确定错误回调实际上没有被调用吗? Place a try-catch block inside the error function. 将try-catch块放在错误函数内。 Remember, this is called asynchronously. 请记住,这被异步调用。 A try-catch block outside of it won't do anything. 外部的try-catch块不会执行任何操作。 Probably the exception is caused when you try to read the error. 当您尝试读取错误时,可能是异常引起的。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM