4

I have a function to validate a code with AJAX(PHP). However, when I try to run it, it returns undefined when it should return true or false.

function:

function verifyViite(viite) {
    $.get('dataminer.php?question=validateViite&q='+viite, function(data) {
        jQuery.globalEval(data);
        if(valid == 1) {
            return true;
        }
        else {
            return false;
        }
    });
}

The data is either valid = 1; or valid = 0;

2 Answers 2

5

You can't return the Callback return value due to the AJAX request being asynchronous.

Execute whatever code calling verifyViite inside the callback itself, instead of returning true or false.

Alternativly, you can have synchronous AJAX request.

5
  • I'm sorry if I understood wrong, but are you saying that I cannot do this validation through a function? Commented Oct 9, 2011 at 12:13
  • You can do this validation through a function, but you rather need to provide a callback(like I posted in my other post..)
    – japrescott
    Commented Oct 9, 2011 at 12:15
  • You can't do it in the way you're trying now - you'll have to change your logic. Post more code and we might be able to give you better logic. Commented Oct 9, 2011 at 12:16
  • one question; Why would one ever want to perform Asynchronous requests? Commented Oct 9, 2011 at 13:20
  • Synchronous request means the browser is waiting until the request is done.. if the server takes long time to load it cause the whole page to "hang" until it finish - not very good experience for the user. By having it asynchronous the page will load as usual and when the data is ready, you can update the proper part in the page. Commented Oct 9, 2011 at 15:15
2

since the function veryViite doesn't return anything, undefined is the expected result. Since its asynchronouse, you would have to give a callback function that would be called on successful ajax call. Something like this;

function verifyViite(viite,cb) {
    $.get('dataminer.php?question=validateViite&q='+viite, function(data) {
        jQuery.globalEval(data);
        if(valid == 1) {
            cb(true);
        }
        else {
            cb(false);
        }
    });
}

and the call would be like;

verifyViite(viite,function(good){
    alert(good);
});
7
  • Ok so cb is a function here? Commented Oct 9, 2011 at 12:21
  • Could you explain to me this "callback" thing? Commented Oct 9, 2011 at 12:27
  • Yes. As you see from how I'm calling verifyViite, I'm providing a function which will be called when the ajax returns. So, the function provided is the variable cb within verifyViite. That function would make more sense if it would count errors and maybe flag a document as "not loadable". At the end of your calls you can then figure out what to do with the errorous calls.. Btw -> using Eval is "bad" practice! JQuery will do all the "eval" neccessary for you to access the information.
    – japrescott
    Commented Oct 9, 2011 at 12:28
  • ok, callbacks -> normally, when you call an asynchronouse function (a function, that can't provide a direct result, cause its doing something in the background and you need to wait), you jump out from the logic of how your code does something. If we provide a function to the asynchronouse function, we can make sure, that function will be called when the asnychronous function returns back. Therefor, it is called "a callback function". helped?
    – japrescott
    Commented Oct 9, 2011 at 12:32
  • Oh, so that function (good) will be called after the server has responded? Commented Oct 9, 2011 at 12:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.