문제

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