简体   繁体   English

PrototypeJS的Ajax错误报告

[英]Ajax Error Reporting With PrototypeJS

I am wondering if there is script or something out there that will report javascript/PrototypeJS errors that happen in Ajax calls. 我想知道是否有脚本或其他东西可以报告在Ajax调用中发生的javascript / PrototypeJS错误。 It can be time consuming to fill code with console.log and/or alerts. 用console.log和/或警报填充代码可能很耗时。 It would be great if there was something like a browser plugin that would do it. 如果有类似浏览器的插件可以做到,那就太好了。

Anybody have any tools or tips? 有人有任何工具或提示吗?

Firefox has Firebug . Firefox具有Firebug
Chrome has Developer Tools (already built in). Chrome具有开发人员工具 (已内置)。
Internet Explorer has Developer Toolbar (already built in). Internet Explorer具有开发人员工具栏 (已内置)。


To catch the errors in script you can use Ajax.Responders , 要捕获脚本中的错误,可以使用Ajax.Responders

Ajax.Responders.register({
  onException: function(request, exception) {
    if (window.console) console.log(exception);
  }
});

You mean like, http errors? 您的意思是,http错误? Some http errors are logged to firebug, like 404 or 500. 一些http错误会记录到Firebug中,例如404或500。

You can extend Ajax.Request and make it report any http response status you want without having to repeat code. 您可以扩展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);
    }
});

Then, instead of calling Ajax.Request, you call Ajax.MyRequest and every ajax call will go through the debugger handlers and through any handler that you want to treat the error individually. 然后,不调用Ajax.Request,而是调用Ajax.MyRequest,每个ajax调用都将通过调试器处理程序以及要单独处理错误的任何处理程序。 Like: 喜欢:

new Ajax.MyRequest("/thisresourcedoesnotexist");

Will throw a 404 and log to console.error. 将抛出404并登录到console.error。

new Ajax.MyRequest("/thisresourcedoesnotexist", {
    on404: function() {
        console.error("But I want to treat this one");
    }
});

Will throw a 404, execute the custom handler and log to console.error. 将抛出404,执行自定义处理程序并登录到console.error。

There is a number of ways to improve this approach. 有许多方法可以改进此方法。 This is just the general idea. 这只是一般的想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM