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