我有一个全局的ajax错误处理程序,即使xhr.status为200,xhr.statusText为“OK”并且xhr.responseText是我的JSON字符串,它仍会运行。这在firefox和IE中发生。
$.ajax({
data: {
method: "getRequestDetails",
loggedInUsername: loggedInUsername,
search: search
},
success: function(data){
var arrayObject = eval("(" + data + ")")['DATA'];
if (arrayObject.length == 0){
alert("That search term returned no results");
} else {
callBeforeShow("Results");
$.each(arrayObject, function(index, value){
showJSON(value, "Results");
});
callAfterShow("Results");
}
}
});
$(document).ajaxError(function(event, XMLHttpRequest, ajaxOptions, thrownError){
var errorMessage = "Ajax Error\n";
errorMessage += "Type: " + ajaxOptions.type + "\n";
errorMessage += "Requesting Page: " + ajaxOptions.url + "\n";
errorMessage += "Status: " + XMLHttpRequest.status + " - " + XMLHttpRequest.statusText + "\n";
errorMessage += "Error Thrown: " + thrownError
alert(errorMessage);
});
在IE中,这表示XMLHttpRequest尚未就绪,而在Firefox中则返回
“AJAX错误” “类型:POST” “请求页面:something.CFC” “状态:200 - 好” “错误抛出:未定义”
所以我的工作是使用
$(document).ajaxComplete(function(event, XMLHttpRequest, ajaxOptions, errorThrown){
if (XMLHttpRequest.status != 200){
var errorMessage = "Ajax Error\n";
errorMessage += "Type: " + ajaxOptions.type + "\n";
errorMessage += "Requesting Page: " + ajaxOptions.url + "\n";
errorMessage += "Status: " + XMLHttpRequest.status + " - " + XMLHttpRequest.statusText;
alert(errorMessage);
}
});
修改 * 这只发生在某些场合。它大部分时间都有效,但有时会运行$ .ajaxError() * 的 EIDT
{"COLUMNS":["ID","SUMMARY_SPEC","TOTAL_EFFORT","EFFORT_HISTORY","LOG_HISTORY"],"DATA":[[816,"test only","2 Minutes - Last Updated: 09\/12\/2010",{"COLUMNS":["CC_ID","RECORD_ID","USER_ID","USER_NAME","EFFORT_DATE","EFFORT_DAYS","EFFORT_HRS","EFFORT_MINS","EFFORT_TOT_HRS"],"DATA":[[816,496,"ruhlet","Tim Ruhle","December, 09 2010 00:00:00",0,0,1,0.0167],[816,497,"ruhlet","Tim Ruhle","December, 08 2010 00:00:00",0,0,1,0.0167]]},{"COLUMNS":["CC_ID","RECORD_ID","USER_ID","USER_NAME","LOG_DT","LOG_ENTRY"],"DATA":[]}]]}{"COLUMNS":["ID","SUMMARY_SPEC","TOTAL_EFFORT","EFFORT_HISTORY","LOG_HISTORY"],"DATA":[[816,"test only","2 Minutes - Last Updated: 09\/12\/2010",{"COLUMNS":["CC_ID","RECORD_ID","USER_ID","USER_NAME","EFFORT_DATE","EFFORT_DAYS","EFFORT_HRS","EFFORT_MINS","EFFORT_TOT_HRS"],"DATA":[[816,496,"ruhlet","Tim Ruhle","December, 09 2010 00:00:00",0,0,1,0.0167],[816,497,"ruhlet","Tim Ruhle","December, 08 2010 00:00:00",0,0,1,0.0167]]},{"COLUMNS":["CC_ID","RECORD_ID","USER_ID","USER_NAME","LOG_DT","LOG_ENTRY"],"DATA":[]}]]}{"COLUMNS":["ID","SUMMARY_SPEC","TOTAL_EFFORT","EFFORT_HISTORY","LOG_HISTORY"],"DATA":[[816,"test only","2 Minutes - Last Updated: 09\/12\/2010",{"COLUMNS":["CC_ID","RECORD_ID","USER_ID","USER_NAME","EFFORT_DATE","EFFORT_DAYS","EFFORT_HRS","EFFORT_MINS","EFFORT_TOT_HRS"],"DATA":[[816,496,"ruhlet","Tim Ruhle","December, 09 2010 00:00:00",0,0,1,0.0167],[816,497,"ruhlet","Tim Ruhle","December, 08 2010 00:00:00",0,0,1,0.0167]]},{"COLUMNS":["CC_ID","RECORD_ID","USER_ID","USER_NAME","LOG_DT","LOG_ENTRY"],"DATA":[]}]]}
最新版本的firebug将其识别为json。
答案 0 :(得分:5)
首先,我强烈建议放弃eval
部分,转而使用$ .parseJson或自动处理解析的dataType:'json'
ajax选项(出于性能和安全等原因) )。如果你继续使用eval
,至少要把它包起来试试。
我不完全确定调用ajaxError的所有情况,但我怀疑(基于错误的“随机性”)它与成功函数内部的错误有关(如eval
在您的回复中收到无效的javascript。这可以解释为什么即使有200响应它也会被调用。
tldr:从eval
回调中获取success
,并在发生此错误时声明您的响应实际上是有效的json。
答案 1 :(得分:3)
可能是因为您没有告诉$.ajax
您希望从服务器返回JSON吗?尝试将dataType
选项设置为'json':
$.ajax({
dataType: 'json',
data: { ...
答案 2 :(得分:1)
当我将json复制并粘贴到此格式化程序/验证程序http://jsonformatter.curiousconcept.com/
时出现两个错误} {。
之间缺少2个逗号答案 3 :(得分:1)
啊,经过几天的搜索,我在$ .ajaxSetup()中注释掉了不同的设置。结果是我设置了超时。如果只有更好的错误消息给出。感谢所有的帮助,如果没有你,就不会在这里。你们都起床了!!!