I have the following ajax request:
我有以下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);
}
}
});
For some reason the error callback is called event though the http status code is 200 ie. the request is OK. Why is this?
由于某种原因,错误回调被称为事件,虽然http状态代码是200即。请求没问题。为什么是这样?
3 个解决方案
#1
10
The problem might be that the json data returned from the url is malformed. When the server actually returns something, the http status code is 200. But that doesn't mean that the data is proper json. Check that the stringified json data returned is correctly formed.
问题可能是从url返回的json数据格式不正确。当服务器实际返回某些内容时,http状态代码为200.但这并不意味着数据是正确的json。检查返回的字符串化json数据是否正确形成。
I'm answering my own guestion because I learned this the hard way. I hadn't escaped a "-quote character in my json data. This resulted in very odd behaviour. Luckily the double quote character is pretty much the only character that needs to be escaped from data delivered via JSON. (More on this issue: Where can I find a list of escape characters required for my JSON ajax return type?)
我正在回答我自己的猜测,因为我很难学到这一点。我没有在我的json数据中转义“-quote”字符。这导致了非常奇怪的行为。幸运的是,双引号字符几乎是唯一需要从通过JSON传递的数据中转义的字符。(更多关于此问题:我在哪里可以找到我的JSON ajax返回类型所需的转义字符列表?)
#2
1
Does your callback return a page with Content-type: application/json
? If not, that could well be the reason.
你的回调是否返回Content-type:application / json的页面?如果没有,那很可能就是原因。
#3
0
I do a lot of testing with file: urls instead of using a web server. My JSON code will always have the wrong MIME type. To take care of this I use the following code:
我使用file:urls进行了大量测试,而不是使用Web服务器。我的JSON代码将始终具有错误的MIME类型。为了解决这个问题,我使用以下代码:
$(document).ready(
function (){
myData = {};
$.ajax({
type: "GET",
// url: "json.php?fn=jsonData.json", // with Apache
url: "jsonData.json", // As a file:/// URL
contentType: "application/json; charset=utf-8",
data: myData,
beforeSend: function(x) {
if(x && x.overrideMimeType) {
x.overrideMimeType("application/json; charset=UTF-8");
}
},
dataType: "json",
success: function(returnData){
$("#jsonData").html("Success:"+returnData.tag);
},
error: function(returnData) {
$("#jsonData").html("Error:"+returnData.tag);
}
});
}
);
This will force the MIME type to be correct for JSON data.
这将强制MIME类型对于JSON数据是正确的。
#1
10
The problem might be that the json data returned from the url is malformed. When the server actually returns something, the http status code is 200. But that doesn't mean that the data is proper json. Check that the stringified json data returned is correctly formed.
问题可能是从url返回的json数据格式不正确。当服务器实际返回某些内容时,http状态代码为200.但这并不意味着数据是正确的json。检查返回的字符串化json数据是否正确形成。
I'm answering my own guestion because I learned this the hard way. I hadn't escaped a "-quote character in my json data. This resulted in very odd behaviour. Luckily the double quote character is pretty much the only character that needs to be escaped from data delivered via JSON. (More on this issue: Where can I find a list of escape characters required for my JSON ajax return type?)
我正在回答我自己的猜测,因为我很难学到这一点。我没有在我的json数据中转义“-quote”字符。这导致了非常奇怪的行为。幸运的是,双引号字符几乎是唯一需要从通过JSON传递的数据中转义的字符。(更多关于此问题:我在哪里可以找到我的JSON ajax返回类型所需的转义字符列表?)
#2
1
Does your callback return a page with Content-type: application/json
? If not, that could well be the reason.
你的回调是否返回Content-type:application / json的页面?如果没有,那很可能就是原因。
#3
0
I do a lot of testing with file: urls instead of using a web server. My JSON code will always have the wrong MIME type. To take care of this I use the following code:
我使用file:urls进行了大量测试,而不是使用Web服务器。我的JSON代码将始终具有错误的MIME类型。为了解决这个问题,我使用以下代码:
$(document).ready(
function (){
myData = {};
$.ajax({
type: "GET",
// url: "json.php?fn=jsonData.json", // with Apache
url: "jsonData.json", // As a file:/// URL
contentType: "application/json; charset=utf-8",
data: myData,
beforeSend: function(x) {
if(x && x.overrideMimeType) {
x.overrideMimeType("application/json; charset=UTF-8");
}
},
dataType: "json",
success: function(returnData){
$("#jsonData").html("Success:"+returnData.tag);
},
error: function(returnData) {
$("#jsonData").html("Error:"+returnData.tag);
}
});
}
);
This will force the MIME type to be correct for JSON data.
这将强制MIME类型对于JSON数据是正确的。