This permits to wire interceptors on XHR requests and responses
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.gitignore Add first stable version Sep 19, 2014
.npmignore Add first stable version Sep 19, 2014
LICENSE Initial commit Sep 19, 2014
README.md Update README.md Sep 19, 2014
index.js add isWired() method Jun 13, 2016
package.json add isWired() method Jun 13, 2016

README.md

ajax-interceptor

This permits to wire some request and response hooks on any Ajax calls.

I find this handy, for exemple, to handle user session expiration and redirect the user to the login page whenever an Ajax call fails with an auth failure.

Install

Install it!

npm install ajax-interceptor --save

Use it! (with Browserify)

var AjaxInterceptor = require("ajax-interceptor");

No other support for now, but feel free to contribute.

API

// Setup some callbacks
AjaxInterceptor.addRequestCallback(function(xhr) {
    console.debug("request",xhr);
});
AjaxInterceptor.addResponseCallback(function(xhr) {
    console.debug("response",xhr);
});

// Will proxify XHR to fire the above callbacks
AjaxInterceptor.wire();

// Do some requests
// ................

// Will restore XHR and not fire anymore the callbacks
AjaxInterceptor.unwire();

You can add and remove callbacks dynamically while the interceptor is wired.

Alternatives

If you just want to be able to intercept JQuery $.ajax() calls, Global Ajax Event Handlers also work.

License

MIT