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

标签 javascript jquery ajax

我目前正在尝试对来 self 的服务器的 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),就好像没有处理程序一样。我如何实现此功能?

最佳答案

错误回调的方法签名有误。参见 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 错误回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39274709/

相关文章:

javascript - 如何仅为 Bootstrap 模式内的选定 div 添加垂直滚动?

jquery - 全日历议程日滚动到第一个事件

javascript - XHR在onreadystatechange中获取请求URL

javascript - 处理传单 map 中的ajax请求

javascript - 总计未进入第二排

javascript - 需要找出 javascript 应用程序中没有日期的计时之间的差异

javascript - 如何将文本字段值从 jsp 传递到 spring Controller 类,反之亦然

javascript - 动画汉堡导航

javascript - 如何通过节点 ID 取消选择 jsTree 复选框

php - 这段 PHP 代码与 Javascript/jQ...怎么样?