5

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.

1
  • 1
    I'm not sure if there are hooks in the $http service for this, but you could easily enough create your own wrapper service around $http to do this. Commented Dec 5, 2012 at 23:01

1 Answer 1

7

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:

  1. 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.

  2. 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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.