Lori
Lori

Reputation: 1422

Is there a way to remove listeners created with ajaxSuccess?

Is there a way to remove listeners created with jQuery's ajaxSuccess?

I would be interested in either ways to remove listeners that happen to be assigned to the variable, or ways to remove all existing ajaxSuccess listeners.

Another angle might be a way to trace back to what request led to the "success" in question, as well as what element-related event triggered that request; ideally analogous to the event.target property in "normal" event listeners. It seems ajaxSuccess' event parameter is a different species of animal and has no target property and no preventDefault method. Many throwers, one catcher, but no catcher identification. Perhaps there is a tool other than ajaxSuccess? Hopefully other than cooking jQuery .ajax from scratch...

Upvotes: 1

Views: 414

Answers (2)

charlietfl
charlietfl

Reputation: 171679

There is no relationship between element events and ajax other than what developer uses to make ajax call.

You can however modify the xhr object while setting up each request and access specific properties in the ajaxSuccess callback. These can be arbitrary properties and values that help you manage your app. For example if you wanted to pass an actual element event to the callbacks

You can also inspect the settings object which will contain the url and use that for conditional code

Simple example:

$.ajax({
    url:'...',
    beforeSend:function(xhr){
       xhr.myData = 'some value';
    }
    ...
});

$( document ).ajaxSuccess(function( event, xhr, settings ) {
    console.log(xhr.myData );
})

Upvotes: 1

guest271314
guest271314

Reputation: 1

I would be interested in either ways to remove listeners that happen to be assigned to the variable, or ways to remove all existing ajaxSuccess listeners

Try using .off()

$(document).off("ajaxSuccess")

Upvotes: 4

Related Questions