2011-09-22 37 views
1

我想知道是否有腳本或其他東西會報告發生在Ajax調用中的javascript/PrototypeJS錯誤。用console.log和/或警報填充代碼可能非常耗時。如果有像瀏覽器插件那樣做,那將是非常棒的。使用PrototypeJS進行Ajax錯誤報告

有人有任何工具或提示嗎?

回答

2

Firefox有Firebug
Chrome已有Developer Tools(已內置)。
Internet Explorer有Developer Toolbar(已內置)。


趕上腳本錯誤,你可以使用Ajax.Responders

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

謝謝,我實際上使用了所有3個工具,但不知道Ajax.Responders。 – fanfavorite

1

你的意思是,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