3

是否可以像这样在我的 JS 中添加其他其他功能:?

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

$.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>");
    }
});​
4

3 回答 3

9
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>");
}​

或者你的意思是:

$.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>");
    }
});​  
于 2012-10-12T03:36:53.680 回答
2

用这个:

$.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");
        }
    }
});
于 2012-10-12T03:38:53.620 回答
0

你也可以做这样的事情

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

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

于 2012-10-12T03:45:30.570 回答