I have a button injected in another website and on click, it is triggering an ajax get request with url, with some included url parameters. Let's say like this:
GET https://example.com/search/test=1&hello=world&session=123
$('#myBtn').click(function() {
// Here
funcForTriggeringAjax()
});
Is there a way to add an call observer on // Here
to start observing only a specific url (something like https://example.com/search/*
) and as soon as I get the result, it stops observing?
So that in the end, I can access to the parameter values.
Totally imaginary scenario:
$('#myBtn').click(function() {
startAjaxObserver()
funcForTriggeringAjax()
});
startAjaxObserver() {
observingStarted(x, result) {
var url = "https://example.com/search/*";
if (x.url == url) {
console.log(result['session'])
stopObservingAjax()
}
}
}
I tried the below answer, but no luck:
$(document).ready(function() {
$('#myBtn').click(function() {
$(document).ajaxSuccess(responseHandler());
});
})
function responseHandler() {
console.log("X") // comes here
return function(event, xhr, settings) {
console.log('Y') // doesn't come here
if (settings.url == 'desired-url' && active) {
// do your thing
active = false;
}
}
}