I would like to manage an ajax call with timeout with .when
and .then
.
I have a script like this:
$.when(
$.ajax({
url: url_ajax,
type: "GET",
async: true,
data: window.location.search,
dataType: "json",
timeout: 30000,
success: function(data) {
console.log('ok');
},
error: function(data) {
}
}),
$.ajax({
url: url_ajax,
type: "GET",
async: true,
data: window.location.search,
dataType: "json",
success: function(data) {
console.log('ok');
},
timeout: 30000,
error: function(data) {
}
})
).then(function() {
alert('end');
});
If an ajax request go in timeout the callback .then
isn't called and I can't see the last alert. So if my ajax request go in timeout I can't exit enter into then
function.
I have also tried to add this after each ajax request:
.fail(function(jqXHR, textStatus){
if(textStatus == 'timeout')
{
alert('Failed from timeout');
}
})
But again doesn't enter into then
function
How can I manage timeout? Thanks
Have you tried using .then(successCallbackFunction, failCallbackFunction)
like so:
$.when(
$.ajax({
url: url_ajax,
type: "GET",
async: true,
data: window.location.search,
dataType: "json",
timeout: 30000,
success: function(data) {
console.log('ok');
},
error: function(data) {
}
}),
$.ajax({
url: url_ajax,
type: "GET",
async: true,
data: window.location.search,
dataType: "json",
success: function(data) {
console.log('ok');
},
timeout: 30000,
error: function(data) {
}
})
).then( // success
function() {
alert('Success!');
},
// failure
function(jqXHR, textStatus){
if(textStatus == 'timeout')
{
alert('Failed from timeout');
} else {
alert('Failed from: '+textStatus);
}
}
);