PrimuS
PrimuS

Reputation: 2683

$.ajax Success - If returns always false

I have a strange error within my $.ajax call here:

//CHECK USERNAME
$('#username').focusout(function() {
    var id = $(this).attr('id');
    var username = ($(this).val());
    $(this).removeClass('hint').removeClass('hint_validated');
    if (!$(this).val() || !regexUser($(this).val())){
        //INVALID
        $('#'+id+'_hint').hide().addClass('hint').show().html(usernameInvalid);
    }else{
        //VALID -> CHECK USERNAME FROM DB IF CONFIG = TRUE
        if (checkUsername == true){
            //LOADING FROM DB
            $('#'+id+'_hint').hide().addClass('hint_check').show().html(usernameCheck);
            $.ajax({
                cache: false,
                type: 'POST',
                url: 'classes/ajax_check.php', //all that does currently is echo "USERNAMEVALID"
                data: {username: username},
                success: function(response){
                    if (response == "USERNAMEVALID") {
                        $('#'+id+'_hint').hide().removeClass('hint_check').addClass('hint_validated').show().html(usernameValid);
                    }else{
                        alert("ERROR");
                    };                      
                },
                error: function(){
                    $('#'+id+'_hint').hide().removeClass('hint_check').addClass('hint').show().html(usernameError);
                }
            });
        }else{
            $('#'+id+'_hint').hide().addClass('hint_validated').show().html(usernameValid);
        }
    }
});

The success function is called but the IF clause throws always FALSE. Why?

If the success function is only alert (response); it actually alerts USERNAMEVALID

I've worked with these functions before but I can't find the error here... Thanks for reading, any help is apreciated.

cheer

PrimuS

Upvotes: 0

Views: 171

Answers (1)

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167182

Check for empty lines. There is a difference between seeing:

alert("Hi\n");
alert("Hi");

You see the same, but it is not. Try this:

alert(encodeURI("Hi\n"));
alert(encodeURI("Hi"));

Where, it alerts this way:

Hi%0A
Hi

PS: If you are using a good browser other than IE, please use console.log() instead of alert(). So that you can check what is printing and what not!

Fiddle: http://jsfiddle.net/XaT6S/

Upvotes: 2

Related Questions