gpt4 book ai didi

javascript - Ajax jqXHR.status==0 修复错误

转载 作者:行者123 更新时间:2023-11-30 12:42:49 25 4
gpt4 key购买 nike

 $.ajax({
url: urlString,
dataType: "json",
type: "GET",
success: function (data) {
alert(data);
},
error: function (jqXHR, exception) {
if (jqXHR.status === 0) {
alert('Not connect.\n Verify Network.');
} else if (jqXHR.status == 404) {
alert('Requested page not found. [404]');
} else if (jqXHR.status == 500) {
alert('Internal Server Error [500].');
} else if (exception === 'parsererror') {
alert('Requested JSON parse failed.');
} else if (exception === 'timeout') {
alert('Time out error.');
} else if (exception === 'abort') {
alert('Ajax request aborted.');
} else {
alert('Uncaught Error.\n' + jqXHR.responseText);
}

}
});

这是我用来从服务器访问某些信息的 javascript 文件。提供的 urlString 是正确的。我所做的是从我正在检索的服务器下载 .json 并在我的计算机上本地访问它。当我从服务器访问文件时,我不断收到 jqXHR.status==0 错误。我不确定如何解决这个问题,因为我看不出我的代码有任何问题。

有人可以指出正确的方向来修复我的错误吗?

最佳答案

您获得不同状态代码的原因是文件不是使用 http: 协议(protocol)获取的,而是使用 file: 协议(protocol)获取的。不同的协议(protocol)有不同的状态码是很自然的。

您只需要根据从何处获取文件而具有不同的行为。

有一个Mozilla bug report about this ,它被标记为无效,因为这被认为是正确的结果。

关于javascript - Ajax jqXHR.status==0 修复错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23802474/

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