Success function in Ajax / jQuery

I'm having trouble displaying the value from the success function of my Ajax call. My code is as follows.

$.ajax({
    type: "POST",
    url: "http://localhost/practical 8/checkuser.php",
    data: form_data,
    success: function(response)
    {
        if(response == 'success'){
            $("#errorUsername").html("<label class='error'>"+response+"</label>");
        }else if(response == 'fail'){
            $("#errorUsername").html("<label class='error'>"+response+"</label>");
        }
    }
});

My checkuser.php is basically echos "succcess" or "fail".

Mine ifand elseif the instructions in my function of success do not work. But do

$.ajax({
    type: "POST",
    url: "http://localhost/practical 8/checkuser.php",
    data: form_data,
    success: function(response)
    {
            $("#errorUsername").html("<label class='error'>"+response+"</label>");
        }
    }
});

works great. What am I doing wrong?

+3
source share
4 answers

I will not criticize if branches leading to the same code, and suppose this is just for testing purposes.

You will need to check the excess free space sent with the PHP script. This explains why javascript conditions do not return true.

trim jQuery: response = jQuery.trim(response);

+5

. :

response = $.trim(response)

if(response == 'success'){
  $("#errorUsername").html("<label class='error'>"+response+"</label>");
}else if(response == 'fail'){
  $("#errorUsername").html("<label class='error'>"+response+"</label>");
}
+1

, . - :

success: function (data, textStatus) {
    if (textStatus === "success" || textStatus === "fail") {
        // do something
    }
}

, , , , .

0

, - URL-, . _ , , URL-. , .

0

All Articles