javascript – 出现错误时未调用jQuery AJAX错误回调

我目前正在尝试实现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://api.jquery.com/jquery.ajax/

根据这些文档的正确签名是:
函数(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语句之外的代码,所以你什么都没看到.

转载注明原文:javascript – 出现错误时未调用jQuery AJAX错误回调 - 代码日志