我有一个全局的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。