skip to Main Content

Is it possible to block an ajax call by its url from another script/js file? I want something like that..

if(ajax_url == 'if_URL_match_then_block_this_ajax_call'){
   //block lower ajax call
}

$.ajax({
    type: "GET",
    url: 'if_URL_match_then_block_this_ajax_call',
    data: {},
    success: function(e) {
      //some event after success...
    }
});

2

Answers


  1. Chosen as BEST ANSWER

    At last I found an answer. I can block a specific url when ajax call start sending request.

    $(document).ajaxSend(function(event, xhr, options) {
      if(options.url == 'if_URL_match_then_block_this_ajax_call'){
        console.log('ajax call abort');
        console.log(event, xhr, options);
        xhr.abort();
      }
    });
    

    Ref: https://api.jquery.com/ajaxSend/


  2. It’s a little hard to tell by the code snippet, but if this code is inside a function you could exit the function early if you detected the URL you didn’t want to send a request for.

    function get(url) {
      if(url === '/some/url/to/block'){
        return;
      }
    
      $.ajax({
        type: "GET",
        url,
        data: {},
        success: function(e) {
          //some event after success...
        }
      });
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search