How can you implement the interceptor pattern on the request / response side on the client side (browser) of AJAX?

Let me start by using in real life:

DWR gets confused when server-side authentication filters try to redirect AJAX requests to the login page due to an expired session. You want to add some filters to

  • Requests with an HTTP status code of 3xx perform client-side redirection, for example window.location = ... login.html
  • A request whose status codes are 2xx are forwarded unchanged to any registered handlers such as DWR.
  • Other codes, such as 4xx , may trigger warnings instead of disappearing into the abyss.

I probably don't need to explain why this type of functionality would be useful; most web server frameworks support the interceptor pattern for the same reasons that can occur on the client.

One (possibly bad) implementation may include moving the raw XMLHttpRequest object to a proxy server that accepts some filter functions. Since jQuery, Prototype, ExtJS, etc. Everyone wraps their own AJAX browser objects, this may be another step.

Can this be implemented initially? What are the technical issues? Has something like this been done before?

+4
source share
1 answer

I implemented something that partially shows up in the jQuery environment. The jQuery AJAX function has the error and success functions, as well as the complete function. So you can do something similar to intercept and redirect based on the returned status code:

 complete: function() { if (data.status == 301 || data.status == 302 || // etc. ) { location.replace('error.html') } 

... etc .. Not quite "initially", but relatively clean from inside jQuery.

0
source

Source: https://habr.com/ru/post/1303106/


All Articles