Tuesday, December 20th, 2005

Debugging Ajax Requests in Prototype

Category: Editorial, Prototype

How do we debug our Ajax applications? The Rails Weenie has taken the Ajax Responder feature in Prototype:

Ajax.Responders.register({
  onCreate: function() {
    if (Ajax.activeRequestCount > 0)
      Element.show('spinner');
  },
  onComplete: function() {
    if (Ajax.activeRequestCount == 0)
      Element.hide('spinner');
  }
});

and has written a simple responder to use in debugging:

Ajax.Responders.register({
  // log the beginning of the requests
  onCreate: function(request, transport) {
    new Insertion.Bottom('debug', '

[' + new Date().toString() + '] accessing ' + request.url + '

') }, // log the completion of the requests onComplete: function(request, transport) { new Insertion.Bottom('debug', '

http status: ' + transport.status + '

' + '
' 
    + transport.responseText.escapeHTML() + '

')
}
});

Posted by Dion Almaer at 8:30 am
4 Comments

+++--
3.1 rating from 8 votes

4 Comments »

Comments feed

This is great! Just note to folks who might not be able to get this working becos of a stupid mistake :)

You need to have a div with id “debug” on the page for it to work since it performs a Insertion.Bottom.

Thanks,
Mandy.

Comment by Mandy — December 20, 2005

Doh, forgot to mention that. Fixed.

Comment by rick — December 20, 2005

LINE FEEDS
It appears like “\n” characters are represented by actual line feeds. I would suggest replacing them with HTML special character representations so the code reads more cleanly.

SINGLE QUOTES
Also, the single quotes represented as either ” ‘ ” or ” ’ ” I believe should be replaced by a standard single quote (” ' “).

I believe the overall representation in the above code was a result of some kind of cut-and-paste effort (translated from text and not escaped for HTML special characters) and not reflective of the actual source. I could be wrong of course. :-)

Comment by Chris Butler — January 17, 2006

Note: Your live preview does not match the output generated by the submission.

Comment by Chris Butler — January 17, 2006

Leave a comment

You must be logged in to post a comment.