1.0.4 • Published 4 years ago
ajax-interceptor-umd v1.0.4
ajax-interceptor-umd
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 by ES Modules
Install it!
npm install ajax-interceptor --save
Use it!
import AjaxInterceptor from "ajax-interceptor-umd";
// or use single
import {addRequestCallback, wire} from "ajax-interceptor-umd";
No other support for now, but feel free to contribute.
Install by UMD
Install it! (Find this js at here)
<script src="./lib/ajax-interceptor-umd.js"></script>
Use it!
window.AjaxInterceptor.addRequestCallback(function(xhr) {
console.debug("request",xhr);
});
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
Hire a freelance expert
Looking for a React/ReactNative freelance expert with more than 5 years production experience? Contact me from my website or with Twitter.