jQuery 在Ajax Jquery函数中添加if else函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12851839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add if else function in Ajax Jquery function
提问by Naga Botak
Is it possible to add other else function in my JS like this: ?
是否可以像这样在我的 JS 中添加其他 else 函数:
if response == success redirect to home
if response == failed redirect to failed
if response == success 重定向到 home
if response == failed 重定向到 failed
$.ajax({
type: "POST",
url: action,
data: form_data,
success: function(response) {
if (response == 'success')
window.location.replace("home");
else
$("#message").html("<div class='error_log'><p class='error'>Invalid username and/or password.</p></div>");
}
});?
回答by gdoron is supporting Monica
success: function(response) {
if (response == 'success')
window.location.replace("home");
else if (response == "failed")
window.location.replace("failed");
else
$("#message").html("<div class='error_log'><p class='error'>Invalid username and/or password.</p></div>");
}?
Or you meant this:
或者你的意思是:
$.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("<div class='error_log'><p class='error'>Invalid username and/or password.</p></div>");
}
});?
回答by Praveen Kumar Purushothaman
Use this:
用这个:
$.ajax({
type: "POST",
url: action,
data: form_data,
success: function(response)
{
if (response == 'success')
window.location.replace("home");
else if (response == 'failure')
{
$("#message").html("<div class='error_log'><p class='error'>Invalid username and/or password.</p></div>");
window.location.replace("failed");
}
}
});
回答by bhb
You can also do something like this
你也可以做这样的事情
$.ajax( "example.php" )
.done(function(msg) {
alert("success");
if(msg == "success") ...
else ...
})
.fail(function() { alert("error"); });
Remember this is only for >jQuery-1.8
请记住,这仅适用于 >jQuery-1.8