Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

detect ajax request by jquery

I'm working with asp controls and I have some dropdownlist which in selection go back to the server and do some action ( via asp ajax ) what I'm l looking for is detecting via jquery when the ajax call is starting I've tried the following call :

  $.ajaxSetup({
                    beforeSend: function (jqXHR, settings) {
                        alert("ok");
                        return false;
                    }
                });

and

also  $(document).ajaxStart(function () {
                   alert("OK");
                });

but none of this worked

like image 415
fatiDev Avatar asked Nov 30 '22 22:11

fatiDev


1 Answers

well, if $(document).ajaxStart(function() {}); is not working for you, try a bit raw js,

var oldXHR = window.XMLHttpRequest;

function newXHR() {
    var realXHR = new oldXHR();
    realXHR.addEventListener("readystatechange", function() {
        if(realXHR.readyState==1){
            alert('server connection established');
        }
        if(realXHR.readyState==2){
            alert('request received');
        }
        if(realXHR.readyState==3){
            alert('processing request');
        }
        if(realXHR.readyState==4){
            alert('request finished and response is ready');
        }
    }, false);
    return realXHR;
}
window.XMLHttpRequest = newXHR;

it should give you all the states of a ajax request and check which one works for you, and then u can remove rest of the if conditions. you can place it outside of $(document).ready(function(){});

like image 141
Ashraful Islam Tushar Avatar answered Dec 04 '22 08:12

Ashraful Islam Tushar