how to call a specific function on every ajax request

ajax, html, javascript, jquery

Solution

Use ajaxSetup, for example

$.ajaxSetup({
    beforeSend: function() {
        console.log('test');
    },
    complete: function() {
        console.log('completed');
    }
});

will setup `beforeSend` handler for every ajax request. Note that `ajaxSetup` can take any option that `$.ajax` can.

Problem

I have a problem, that I have several pages in my project and I used a lot of ajax requests in my project, but now I think that whenever an ajax request is called a function will called and whenever that request ends another function will call. How can I do this globally I know I can put this in every ajax request but I need a solution which I do in one place and it works all over the project. ``` $(document).read(function(){ // Suppose this document load function is written on layout page and every page is inherited from this page }); ```

Original source