jQuery AJAX在出错时未调用错误回调函数

4

我目前正在尝试从我的服务器处理 HTTP 413:请求实体过大 错误。我所做的是:

$.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),似乎没有处理程序触发错误回调。我该如何实现这个功能?


请尝试查看以下链接:http://craftcms.stackexchange.com/questions/2328/413-request-entity-too-large-error-with-uploading-a-file 或者 http://stackoverflow.com/questions/37489351/413-request-entity-too-large-error-during-file-upload-php - vaso123
@karacsi_maci 谢谢,这绝对是一个好的选择。如果可能的话,我更愿意在客户端处理它,但如果证明太困难,那么我将不得不改变服务器设置。 - Bluefire
2个回答

3

您的错误回调函数方法签名不正确。请参阅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相同... - Bluefire
太棒了!控制台还是报错,但至少现在回调函数已经触发了。谢谢你! - Bluefire
1
控制台错误信息是什么?大多数浏览器会将HTTP错误作为常规信息报告到控制台中,这并不完全在您的控制范围内。错误回调的目的是让您的应用程序能够优雅地处理它,而不是消除控制台错误。附言:如果答案有帮助,请不要忘记将其标记为已接受 :-) - ADyson

1
你可以使用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);
            }
        }
    });
});

1
这是一个不错的解决方案,虽然它并不适用于我当前的情境,因为我想要处理一些当时当地的数据,但这绝对是一个我以后会注意到的功能。 - Bluefire

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接