Deferrentと、この特定のケースでの使用方法に関する問題に直面しています。
シナリオは次のとおりです。
サーバーにデータを送信するループがあります。例えば:
var array = [{"effect":"save","params":["login_0_form_form_login"],"url":"http://www.example.local/login"},{"effect":"redirect","params":["index"],"url":"http://www.example.local/index"}];
$.each(array,function(key,payload) {
$.post(payload.url,payload);
});
次に、ajax呼び出しの結果を処理するsuccessメソッド。
App.success = function(result){
//Here I am deciding what to do with the result and checking for errors or warnings
if(result.notification.success) {
//Everything is good
}
else if(result.notification.error) {
//Something is wrong, somehow stop the other ajax call
}
}
それにもかかわらず、私が直面している問題は、サーバーがエラー(たとえば、不正なログインデータ)を返す可能性があることです。このシナリオでは、サーバーから不正なログインデータが返されますが、リダイレクトされます。前のリクエストがエラーを返したかどうかをどうにかして制御する必要があります。はいの場合は、投稿を続けないでください。
$ .when関数内で、$。thenを使用して、$。postを返そうとしました。しかし、私は自分が望んでいたことを達成できませんでした。