views:

70

answers:

2

I have a JavaScript method that call JQuery.get() and i want to return value from itto callee function .. the following is the code :

function CheckExistance(userName) {
        $.get(
            "JQueryPage.aspx", { name: userName },
             function(result) {
                 if (result == "1") {
                     value = true;
                 }
                 else if (result == "0") {
                     value = false;
                 }
                 else if (result == "error") {
                     value = false;
                 }
             }
        );                
        return value;
    }

i want return value; return the value setted in get() function ... i know that get() is asynchronous operation anf i want a solution to do that ?

+5  A: 

You cannot "return" the value into the called function; because as you've realised, the AJAX call is asynchronous. Instead, you need to effectively delay the execution of the code that follows the AJAX call; by placing this code in the callback function instead.

What happens now, is that this code is executed when the AJAX request has completed, and the value is available.

function CheckExistance(userName, callback) {
        $.get(
            "JQueryPage.aspx", { name: userName },
             function(result) {
                 var value = false;

                 if (result == "1") {
                     value = true;
                 }


                 callback(value);
             }
        );                
    }

Then where your function might previously have been:

function validateUserName() {
    var input = $('someField').val();
    var isUnique = CheckExistance(input);

    if (isUnique) {
       // whatever
    } else {
       alert("This username is already taken");
    };
};

Should now be:

function validateUserName() {
    var input = $('someField').val();

    CheckExistance(input, function (isUnique) {
       if (isUnique) {
          // whatever
       } else {
          alert("This username is already taken");
       };
    });
};
Matt
+1 for the correct answer. Callback is the way to go.
Felix Kling
Thanks for your fast response , that's what i need
Space Cracker
sorry but i have another question related to this solution if i want in onClick event of button in which if result = false i prevent submit else do submit i do the function but it also executed before validateUserName() finished
Space Cracker
i found a solution by replacing get by ajax like thisvar isAvailable = false; $.ajax({ async: false, url: "JQueryPage.aspx", data: "name=" + username, success: function(result) { ....
Space Cracker
I would **strongly** suggest not using synchronous AJAX requests. The whole page locks up whilst the request is in progress. You cannot (without making the AJAX synchronous) return true/false from the AJAX request in time to return false from the `onClick` handler. You can either return false, then call `form.submit()` in the AJAX callback function, or simply do all this work on the server side.
Matt
but i can't do check onClick of submit button as asynchronous will not return value before check .. so how i can do with make it synchronized ?
Space Cracker
You don't. You `return false` immediately when the user submits the form, then once the AJAX call has completed, you can call `theForm.submit()` (`$(formSelector).get(0).submit()`) in jQuery if you want to submit the form.
Matt
A: 

This might work;

function CheckExistance(userName) {
    var value
    $.get(
        "JQueryPage.aspx", { name: userName },
         function(result) {
             if (result == "1") {
                 value = true;
             }
             else if (result == "0") {
                 value = false;
             }
             else if (result == "error") {
                 value = false;
             }
         }
    );
while(value == undefined);
    return value;
}
Eric
No it doesn't. First, I bet you meant `value != undefined` and second, this will **block** the client. Then you could also do an synchronous call, this will definitely work but who in the world wants synchronous calls ;)
Felix Kling
Your while loop won't work: `while (value === undefined) ; return value;` would work, but is probably the most undesirable solution.
Matt
even if it work it can freeze client if any problem
Space Cracker
@Felix: No, I forgot the semicolon. Oh, and the OP seemed to want a synchronous call.@Matt: What benefit does `===` offer over `==`
Eric
@Eric: It doesn't. I was commenting on the missing semi colon.
Matt
Oh ok. I thought that you were making two points
Eric