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

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

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>");
    }
});​  
Sign up to request clarification or add additional context in comments.

8 Comments

I think that success is deprecated in favour of done api.jquery.com/jQuery.ajax
@DavidJohnWelsh. Hell no! Why do you think so?
@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.
I guess I misunderstand the difference... so success : function (data) {} != .done(function(data) {})? I thought they performed the same function :-/
|
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");
        }
    }
});

Comments

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

1 Comment

This doesn't work, as the response is always success but based on the response, you need to execute something.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.