Вопрос

I have a call thus:

$.ajax({url:"url", ....}).then(function(response) {
  if (response.sucess) {
    return $.ajax({.....});
  }
  else {
    //what do I return here so the next then doesn't fire but no error is raised?
  }
}).then(function(response2) {
   //....Do more stuff
});

So the first ajax call returns a success flag. If this succeeds then I want it to continue. If it doesn't I want to prevent the next then from firing. I'm unsure what I should return from the first then when success == false?

I feel like I should return a $.Deferred as this is what then expects but I'm not sure how to do this being as I'm not firing another deferred action.

Это было полезно?

Решение

Figured it out. Need to return a .reject();

$.ajax({url:"url", ....}).then(function(response) {
  if (response.sucess) {
    return $.ajax({.....});
  }
  else {
    return $.Deferred().reject();
  }
}).then(function(response2) {
   //....Do more stuff
});
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top