3

Is it possible to add other else function in my JS like this: ?

if response == success redirect to home
if response == failed redirect to 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>");
    }
});​
gdoron
  • 147,333
  • 58
  • 291
  • 367
Naga Botak
  • 721
  • 2
  • 9
  • 14

3 Answers3

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

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>");
    }
});​  
gdoron
  • 147,333
  • 58
  • 291
  • 367
  • I think that `success` is deprecated in favour of `done` http://api.jquery.com/jQuery.ajax/ – David John Welsh Oct 12 '12 at 03:43
  • @DavidJohnWelsh. Hell no! Why do you think so? – gdoron Oct 12 '12 at 03:45
  • @DavidJohnWelsh You probably meant this: *Deprecation Notice: The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callbacks will be deprecated in jQuery 1.8. To prepare your code for their eventual removal, use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead.* It applies only for the `jqXR` obect returen by ajax, not to the success and error properties. – gdoron Oct 12 '12 at 03:52
  • I guess I misunderstand the difference... so `success : function (data) {}` != `.done(function(data) {})`? I thought they performed the same function :-/ – David John Welsh Oct 12 '12 at 04:56
  • Never mind, I got it... http://stackoverflow.com/questions/8840257/jquery-ajax-handling-continue-responses-success-vs-done – David John Welsh Oct 12 '12 at 04:58
  • @DavidJohnWelsh. done will fire on error as well, success only on... success. in other words, done catches both success and error events. – gdoron Oct 12 '12 at 04:58
  • `done` fires on error? What kind of error? It doesn't fire if the ajax request fails; that is what `fail` and `always` are for. Getting confused again.... – David John Welsh Oct 12 '12 at 05:03
  • @DavidJohnWelsh. I'm not familiar with the 1.8.* changes, you can simply read the docs. :) – gdoron Oct 13 '12 at 19:55
2

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");
        }
    }
});
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
0

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

bhb
  • 2,476
  • 3
  • 17
  • 32