在Ajax Jquery函数中添加if else函数

是否有可能在我的JS中添加其他function:

如果响应==成功重定向到家
如果响应==失败重定向失败

$.ajax({ type: "POST", url: action, data: form_data, success: function(response) { if (response == 'success') window.location.replace("home"); else $("#message").html("

Invalid username and/or password.

"); } });​

 success: function(response) { if (response == 'success') window.location.replace("home"); else if (response == "failed") window.location.replace("failed"); else $("#message").html("

Invalid username and/or password.

"); }​

或者你的意思是:

 $.ajax({ type: "POST", url: action, data: form_data, error: function() { window.location.replace("Failed"); }, success: function(response) { if (response == 'success') window.location.replace("home"); else $("#message").html("

Invalid username and/or password.

"); } });​

用这个:

 $.ajax({ type: "POST", url: action, data: form_data, success: function(response) { if (response == 'success') window.location.replace("home"); else if (response == 'failure') { $("#message").html("

Invalid username and/or password.

"); window.location.replace("failed"); } } });

你也可以这样做

 $.ajax( "example.php" ) .done(function(msg) { alert("success"); if(msg == "success") ... else ... }) .fail(function() { alert("error"); }); 

请记住,这仅适用于> jQuery-1.8