gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:42:33 24 4
gpt4 key购买 nike

我目前正在尝试对来 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com