views:

142

answers:

0

Let me start with a real-life use case:

DWR is getting confused when server-side authentication filters attempt to redirect AJAX requests to the login page due to an expired session. You'd like to add some filters so that

  1. Requests whose HTTP status code equal 3xx execute a client-side redirect, like window.location = ...login.html
  2. Request whose status codes equal 2xx are forwarded on - unchanged - to any registered handlers, like DWR.
  3. Other codes, like 4xx might trigger alerts instead of disappearing into the abyss.

I probably don't have to explain why this type of functionality would be useful; most server-side web frameworks support the interceptor pattern for similar reasons you might want it on the client.

One (probably bad) implementation could involve wrapping the raw XMLHttpRequest object in a proxy which accepts some filter functions. Since jQuery, Prototype, ExtJS, etc. all wrap the native browser AJAX objects already, this could be one additional step.

This this possible to implement natively? What are the technical challenges? Has anything like this been done before?