gpt4 book ai didi

jquery - 为什么 $.ajax 调用 json 数据会在 http 状态码为 "200 OK"时触发错误回调?

转载 作者:行者123 更新时间:2023-12-03 22:38:17 25 4
gpt4 key购买 nike

我有以下ajax请求:

        jQuery.ajax({
async: true,
type: "GET",
url: url,
data: data,
dataType: "json",
success: function(results){
currentData = results;
},
error: function(xhr, ajaxOptions, thrownError){
if (xhr.status == 200) {
console.debug("Error code 200");
}
else {
currentData = {};
displayAjaxError(xhr.status);
}
}
});

由于某种原因,错误回调被称为事件,尽管http状态代码是200,即。请求没问题。这是为什么?

最佳答案

问题可能是从 url 返回的 json 数据格式错误。当服务器实际返回一些东西时,http状态码是200。但这并不意味着数据是正确的json。检查返回的字符串化json数据格式是否正确。

我正在回答我自己的问题,因为我是通过惨痛的教训才学到这一点的。我没有在 json 数据中转义“-quote 字符。这导致了非常奇怪的行为。幸运的是,双引号字符几乎是唯一需要从通过 JSON 传递的数据中转义的字符。(有关此问题的更多信息: Where can I find a list of escape characters required for my JSON ajax return type?)

关于jquery - 为什么 $.ajax 调用 json 数据会在 http 状态码为 "200 OK"时触发错误回调?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1846086/

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