0

I've got this code:

function fetchSocialCount(type,fileSrc,callback){
    var req = new XMLHttpRequest();
    req.onload = function(){
        if(req.status === 200 && req.readyState === 4){
            var countResponse = JSON.parse(req.responseText);
            callback(countResponse);
        }
    }
    req.open("GET","../scripts/php/returnSocialCount.php",false);
    req.send();
}
var test = fetchSocialCount("img","ez.png",function(count){
    return count;
});

console.log(test);

But for some reason, test is undefined. My AJAX does work, and it returns an object. What's the problem?

5
  • @Pointy I've read that answer a couple of times now, and AFAIK it doesn't have a solution to my problem, as it's different. Commented Oct 7, 2015 at 16:28
  • The "onload" handler for your will be called by the system when the request completes, but the system doesn't pay attention to the return value. Your handler doesn't have any return statements in it anyway. Commented Oct 7, 2015 at 16:29
  • True, this is not an async situation (though you'll find that browsers will soon stop supporting synchronous xhr, if they haven't already). However, neither your "fetch" function nor your "onload" handler have return statements, and even if they did the runtime wouldn't pass along any return value from the "onload" handler. Commented Oct 7, 2015 at 16:31
  • @Pointy Yes, but that's why I have a callback, that does have a return statement, that for some reason returns undefined, and therefore you cannot mark my question as a duplicate, as it really isn't one. Commented Oct 7, 2015 at 16:34
  • 1
    @aCodingN00b - but the callback returns to that which called the callback, (in this case callback(countResponse);), no further up. Commented Oct 7, 2015 at 16:37

1 Answer 1

4

Your test gets the return value of fetchSocialCount itself, which is undefined since it doesn't return anything.

You can try this:

function fetchSocialCount(type, fileSrc) {
    var req = new XMLHttpRequest()
        , result = null;

    req.onload = function(){
        if(req.status === 200 && req.readyState === 4){
            result = JSON.parse(req.responseText);
        }
    }
    req.open("GET","../scripts/php/returnSocialCount.php",false);
    req.send();

    return result;
}

Since you are using a synchronous request, you should be able to return its result directly; returning from the callback as you did won't help anyway, in fact, as @Trolleymusic points out in the comments

the callback returns to that which called the callback, (in this case callback(countResponse)), no further up

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks. Works perfectly.
@Pointy Thanks, I'll look into it.
@aCodingN00b if I may ask, why did you un-accept the answer? Is there anything I can do to improve it and/or make it more useful?
@MatteoTassinari I'm sorry, I accepted it, and I accidentally clicked it again, then it bugged out (I couldn't accept it again). Sorry, I accepted it again now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.