0
function check_username(){
    $.ajax({
        type: "POST",
        dataType: 'json',
        url: "/ajax/check/username.html",
        data: "via=ajax&username="+$('input[name=register_username]').val(),
        success: function(msg){
            if(msg.response==false){
                register_username.parent().css('background-color','#db2e24');
                register_username.parent().parent().find('td:last-child').text(msg.message);
                register_username.focus();
                return false;
            } else {
                register_username.parent().css('background-color','#fff');
                register_username.parent().parent().find('td:last-child').text("");
                return true;
            }
       }
    });
}

I'm sorry if my English isn't good -- English is not my native language. Back to the topic, why does the function above always return false? FYI : the JSON is OK

jamesplease
  • 12,547
  • 6
  • 47
  • 73
Bias Tegaralaga
  • 2,240
  • 5
  • 21
  • 26
  • 1
    because it returns `undefined`(nothing) which is falsey – Linsey Sep 27 '11 at 03:12
  • 1
    Are you sure it's not returning `undefined`? The `check_username()` function doesn't have a return statement so it will return undefined. The return statements that you have inside the success handler function are for _that_ function, not `check_username()`. – nnnnnn Sep 27 '11 at 03:13
  • It actually returns the reponseText from the Ajax call, you got the function parameters wrong. – b01 Sep 27 '11 at 03:14
  • so, how to pass statements inside the success handler to check_username() i'm sorry, if i ask too much i'm noob in javascript T_T – Bias Tegaralaga Sep 27 '11 at 03:15
  • Your probably just have invalid JSON, check my answer below. – b01 Sep 27 '11 at 04:01

3 Answers3

4

check_username calls an ajax function which starts a networking operation and then returns immediately. check_username returns long before the ajax call finishes and the success handler gets called. Thus, the success handler has NOTHING to do with the value that check_username returns.

Since there is no return value in the check_username function itself (only in the embedded success handler function), check_username returns undefined which is a falsey value, thus you think it's always returning false.

If you want to do something with the return value from the success handler, then you have to either operate in the success handler itself or you have to call another function from the success handler. This is how asynchronous operations work.

Returning true or false from the success handler function does nothing. The success handler is called by the internals of the ajax handling code and returning from the success handler just goes into the bowels of the ajax internals. The return value from the success handler is not used in any way.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • so, how do i pass the statement from success handler into check_username() ? – Bias Tegaralaga Sep 27 '11 at 03:21
  • You can't. `check_username` has already completed when the success handler is called. Whatever you want to do with the returned data, you can just do in the success handler or you can call some other function and pass it the ajax data. – jfriend00 Sep 27 '11 at 03:30
0

The problem is the logical condition msg.response==false.. this always evaluates to false, the response is not a boolean. You need to check the response status instead.

Anas Karkoukli
  • 1,342
  • 8
  • 13
0

If I had to guess, you post, and the response returned is a JSON object. But it looks like your just checking to see if you got a valid response. Why not try it this way:

Try this:

function check_username()
        {
            $.ajax({
                type: "POST",
                dataType: 'json',
                url: "/ajax/check/username.html",
                data: "via=ajax&username="+$('input[name=register_username]').val(),
                success: function( msg, textStatus )
                {
                    if ( textStatus == "success" )
                    {
                        register_username.parent().css('background-color','#db2e24');
                        register_username.parent().parent().find('td:last-child').text(msg.message);
                        register_username.focus();
                        return false;
                    } else {
                        register_username.parent().css('background-color','#fff');
                        register_username.parent().parent().find('td:last-child').text("");
                        return true;
                    }
                },
                error: function( jqXHR, textStatus, errorThrown )
                {
                    if ( textStatus == "parsererror" )
                    {
                        alert( "There is an error in your JSON object" );
                    }
                }
            });
        }

The other problem you could have is that your not returning valid json, which jQuery will check. Adding an error will help reveal if this is indeed the case.

Live demo http://jsfiddle.net/khalifah/4q3EJ/

b01
  • 4,076
  • 2
  • 30
  • 30
  • 2
    This answer is *WRONG* since the success function is called asynchronously. -- The success function is called by the Ajax machinery, it is not part of the stack/call thread that includes the check_username function. You could change the ajax call to be synchronous, but that'd be a very very poor UX. – Larry K Sep 27 '11 at 03:20
  • jQuery returns "success" method returns a second parameter that indicates how the request went. The only thing I had wrong is that I thought it was 200 on success, but it actually returns the string "success". You can try it out to see for your self. – b01 Sep 27 '11 at 03:31
  • 200 is the response code, I had it wrong, it returns the string "success" if the request went well. – b01 Sep 27 '11 at 03:33
  • i'm pretty sure my json is ok – Bias Tegaralaga Sep 27 '11 at 04:07