JavaScript Generic way to handle all AJAX calls asp.net generic handler ajax call,ajax call generic handler,

Is there a way to detect all AJAX calls (both GET and POST)? I need to do a generic way to show a loading div while the AJAX call process are running. Something like the code below:

$.ajax({
   url: 'my/url',
   type: "GET",
   dataType: "json",
   beforeSend: function() {
       $('#loading').show();
   },
   success: function() {
       $('#loading').hide();
       // do some stuff...
}

Instead to call in every AJAX beforeSend() and success() behaviors (show and hide the loading div), I'm searching a generic way to handle it. When I have an AJAX call, I just do it:

$.ajax({
   url: 'my/url',
   type: "GET",
   dataType: "json",
   success: function() {
       // do some stuff...
}

When that beforeSend() behavior is implicity in this request and the same for the success() hide behavior. Do you have any idea how can I treat this thing?

Thank you all!

Answer:1

Yes, you can do this using .ajaxStart() & .ajaxStop() methods like:

$(document).ready(function () {
    $(document).ajaxStart(function () {
        $('#loading').show();
    }).ajaxStop(function () {
        $('#loading').hide();
    });
});
Answer:2

I have a form which has: two input text fields 4 selects 1 textarea Is there an easy way to capture the following event? When a user types inside the input fields, or selects something from the ...

I have a form which has: two input text fields 4 selects 1 textarea Is there an easy way to capture the following event? When a user types inside the input fields, or selects something from the ...

  1. javascript know event listeners

I have an array of async functions, where it only makes sense to run one if the previous one has been resolved. You can think of them as HTT get requests to different URL, e.g. $http.get('/step1') $...

I have an array of async functions, where it only makes sense to run one if the previous one has been resolved. You can think of them as HTT get requests to different URL, e.g. $http.get('/step1') $...

I have a filedset and legend inside that with "some text" and inside this fieldset i have a GRID I have 2 questions How to show ? hide html filedset from code behind, i tried the following way to ...

I have a filedset and legend inside that with "some text" and inside this fieldset i have a GRID I have 2 questions How to show ? hide html filedset from code behind, i tried the following way to ...

  1. show hide html element
  2. show hide html element based on checkbox control
  3. show hide html element jquery
  4. show hide html elements using jquery
  5. show hide html css
  6. show hide html code
  7. show hide html button
  8. show hide html table using javascript
  9. show hide html
  10. show/hide html table columns using css
  11. show hide html javascript
  12. show hide html label javascript
  13. show hide html div
  14. show hide html element angular
  15. show hide html jquery
  16. show hide html element css
  17. show hide html control using javascript
  18. show hide html table
  19. show hide html attribute
  20. show hide html table rows

This is the link that I have: href='" . $ajax_like_link . "' data-task='like' data-post_id='" . $post_id . "' data-nonce='" . $nonce . "'>"; And I want to replace the displayed link value from ...

This is the link that I have: href='" . $ajax_like_link . "' data-task='like' data-post_id='" . $post_id . "' data-nonce='" . $nonce . "'>"; And I want to replace the displayed link value from ...