Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add if else function in Ajax Jquery function

Tags:

jquery

ajax

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>");
    }
});​
like image 254
Naga Botak Avatar asked Dec 06 '25 04:12

Naga Botak


2 Answers

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>");
    }
});​  
like image 93
gdoron is supporting Monica Avatar answered Dec 08 '25 18:12

gdoron is supporting Monica


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");
        }
    }
});
like image 26
Praveen Kumar Purushothaman Avatar answered Dec 08 '25 17:12

Praveen Kumar Purushothaman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!