【问题标题】:jQuery AJAX error callback not being called when there is an error发生错误时未调用jQuery AJAX错误回调
【发布时间】:2016-09-01 14:48:46
【问题描述】:

我目前正在尝试处理来自我的服务器的 HTTP 413: Request entity too large 错误。我所做的是这样的:

$.ajax({
    url: "submit.php",
    data: {
        "data": POSTData
    },
    success: function(response, statusText, XHR) {
        console.log(XHR.status + ": " + response);
        resolve(); // resolve the promise and continue on with execution
    },
    // Added this part:
    error: function(response, statusText, XHR) {
        if(XHR.status === 413) {
            // Request entity too large
            // To solve this we split the data we want to upload into several smaller partitions
            // and upload them sequentially

            console.log("Error 413: Request entity too large. Splitting the data into partitions...");

            // handling code below

            // blahblahblah
        }
    },
    method: "POST"
});

但是,我的控制台并没有触发错误回调,而是仍然抛出一个错误(它说是 413),就好像没有处理程序一样。如何实现此功能?

【问题讨论】:

标签: javascript jquery ajax


【解决方案1】:

错误回调的方法签名错误。见http://api.jquery.com/jquery.ajax/

根据这些文档,正确的签名是: Function(jqXHR jqXHR, String textStatus, String errorThrown)

因此在您的情况下 XHR.status 不存在,因为您所谓的 XHR 实际上是一个字符串。

试试这个:

 error: function (jqXHR, textStatus, errorThrown) {
    if(jqXHR.status === 413) {
        // Request entity too large
        // To solve this we split the data we want to upload into several smaller partitions
        // and upload them sequentially

        console.log("Error 413: Request entity too large. Splitting the data into partitions...");

        // handling code below

        // blahblahblah
    }
},

我强烈怀疑错误回调 正在 被调用,但是因为您在 if 语句之外没有代码,所以您什么也看不到。

【讨论】:

  • 谢谢!我会试试看。我只是假设error的签名与success的签名相同...
  • 太棒了!我仍然在控制台中收到错误,但至少现在回调正在触发。谢谢!
  • 控制台错误说明了什么?大多数浏览器在控制台中报告 HTTP 错误是理所当然的,它并不真正在你的控制之中。错误回调的目的是让您的应用程序可以优雅地处理它,而不是消除控制台错误。附:如果答案有帮助,请不要忘记将其标记为已接受:-)
【解决方案2】:

您只能在一个地方使用jQuery.ajaxSetup() 处理此类错误一次。这样您可以处理多个HTTP 413。

代码:

$(function() {
    $.ajaxSetup({
        error: function(jqXHR, exception) {
            if (jqXHR.status === 0) {
                console.log('Not connect.n Verify Network.');
            } else if (jqXHR.status == 404) {
                console.log('Requested page not found. [404]');
            } else if (jqXHR.status == 413) {
                console.log('Error [413]: Request entity too large. Splitting the data into partitions...');
            } else if (jqXHR.status == 500) {
                console.log('Internal Server Error [500].');
            } else if (exception === 'parsererror') {
                console.log('Requested JSON parse failed.');
            } else if (exception === 'timeout') {
                console.log('Time out error.');
            } else if (exception === 'abort') {
                console.log('Ajax request aborted.');
            } else {
                console.log('Uncaught Error.n' + jqXHR.responseText);
            }
        }
    });
});

【讨论】:

  • 这是一个不错的解决方案,虽然它不适合我当前的环境,我想处理当时和那里的一些数据,但它绝对是我稍后会注意的一个功能。
猜你喜欢
  • 2011-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-11
  • 1970-01-01
  • 1970-01-01
  • 2012-06-20
相关资源
最近更新 更多