Reputation: 1567
I have an ajax request written in an object, I need to write a function which cancels this specific request. I've looked at the abort method but not sure how to get it to work this way.
getItems: function(categoryID, format, numberOfResults, types, sortBy, searchString){
var url = host+"getItems?categoryID="+categoryID+"&format="+format+"&numberOfResults="+numberOfResults+"&types="+types+"&sortBy="+sortBy+'&fileName='+searchString;
return $.ajax({
type:"get",
dataType:"html",
url: url,
success:function(data){
resItems = data;
},
error: function(data){
console.log(data);
}
});
},
The problem I have is this function is called when certain elements are called, I need to cancel this request before initiating the next one if the user clicks on two elements in quick succession.
The page has multiple ajax requests so I can't do a blanket cancel all but need to cancel this one specifically.
The structure for the object is like this:
var items: {
getItems: function(){
},
getItemInfo: function(){
}
}
and so on.
What I would like to do is target an ajax request based on its request URL and cancel it, is that possible from an external function?
Upvotes: 0
Views: 49
Reputation: 17906
because getItems is returning the XMLHttpRequest it should be as simple as
resp = items.getItems(params)
resp.abort()
Upvotes: 1