Wednesday, November 9, 2022

[FIXED] how to call a specific function on every ajax request

Issue

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
});

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.



Answered By - freakish
Answer Checked By - Robin (PHPFixing Admin)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.