I've browsed through the documentation but couldn't find anything mentioning this. I'm using Angular's $http service and want to run code before every ajax event, and after every ajax event (to display a loading message while waiting for the server to respond).
I have jQuery included and tried doing:
$(document).ready(function() {
$('body').ajaxSend(function(event, jqXHR) {
console.log('in jquery send');
}) ;
});
But couldn't get the message logged to the console. Any help is appreciated.
Please check out the docs on $http service: http://docs.angularjs.org/api/ng.$http
You will see that there are two options that you can use:
httpInterceptor (see Response interceptors section in the docs) - it allows you to wrap every response and do whatever you want before it completes the request. It uses promises so it's really easy to even do some async stuff after each request.
request/response transformations (see Transforming Requests and Responses) - it allows you to process every request and response that goes through $http - so that would be my choice as it allows you to hook before and after the request.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With