在jQuery的1.5 +,您可以通过converters
选项$.ajax
做到这一点。通常情况下,转换器只是将从Web服务器接收到的数据转换成成功回调,但如果需要,您可以在其中运行其他代码。
将此代码放入嵌入在所有页面上的js文件中。
$.ajaxSetup({
// the converter option is a list of dataType-to-dataType conversion functions
// example: converters['text json'] is a function that accepts text and returns an object
// note: to redefine any single converter, you must redefine all of them
converters: {
"text json": function (text) {
// NOTE: this converter must return a parsed version of the json
var result = jQuery.parseJSON(text);
if (result.errorMessage) {
// catch the error message and alert
alert(result.errorMessage)
}
return result;
},
"text html": true,
"* text": window.String,
"text xml": jQuery.parseXML
}
});
完整的示例: http://jsfiddle.net/waltbosz/8a8fZ/
全局AJAX的事件处理程序成功后的回调(例如)被解雇。如果OP在实际成功被解雇之前想要做一些事情,他将无法使用全局事件处理程序来完成。 – 2013-04-09 23:37:34