使用PrototypeJS进行Ajax错误报告
问题描述:
我想知道是否有脚本或其他东西会报告发生在Ajax调用中的javascript/PrototypeJS错误。用console.log和/或警报填充代码可能非常耗时。如果有像浏览器插件那样做,那将是非常棒的。使用PrototypeJS进行Ajax错误报告
有人有任何工具或提示吗?
答
Firefox有Firebug。
Chrome已有Developer Tools(已内置)。
Internet Explorer有Developer Toolbar(已内置)。
赶上脚本错误,你可以使用Ajax.Responders
,
Ajax.Responders.register({
onException: function(request, exception) {
if (window.console) console.log(exception);
}
});
答
你的意思是,http错误?一些http错误被记录到萤火虫,如404或500.
您可以扩展Ajax.Request并使其报告任何http响应状态而不必重复代码。
Ajax.MyRequest = Class.create(Ajax.Request, {
initialize: function($super, url, options) {
function debuggerHandler(response) {
if(console && console.error) {
console.error("ERROR: " + response.responseText);
}
}
var debuggers = ["onFailure"]; //add any custom http status code
options = Object.clone(options) || {};
var handler;
var old;
for(var d in debuggers) {
handler = debuggers[d];
if(options[d]) {
old = options[d];
handler = function(response) {
old(response);
debuggers[d](response);
}
}
options[d] = handler;
}
$super(url, options);
}
});
然后,而不是调用的Ajax.Request,你叫Ajax.MyRequest和每一个Ajax调用会经过调试处理程序,并通过要单独对待错误的任何处理。像:
new Ajax.MyRequest("/thisresourcedoesnotexist");
会抛出一个404并记录到console.error。
new Ajax.MyRequest("/thisresourcedoesnotexist", {
on404: function() {
console.error("But I want to treat this one");
}
});
会抛出404,执行自定义处理程序并记录到console.error。
有许多方法可以改进这种方法。这只是一般的想法。
+0
谢谢,看起来像它会工作,但更简单的方法似乎是使用内置的函数原型。 – fanfavorite
谢谢,我实际上使用了所有3个工具,但不知道Ajax.Responders。 – fanfavorite