Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I've this function

function getTags(level){
    $.getJSON("php/get-tags.php", { "parent": level }, function(json) {
        return json;
    });
}

I'm calling this function as

$(function(){
    var tags = getTags('0');  
});

The problems is, in the function getTags() the return json is like

{"tags":["Mathematics","Science","Arts","Engineering","Law","Design"]}

but at var tags = getTags('0'), catching the return value, it gets an undefined value.

Is the way I'm returning the value incorrect?

share|improve this question
1  
possible duplicate of JavaScript asynchronous return value / assignment with jQuery –  Felix Kling Jan 11 '12 at 21:10
add comment

5 Answers

up vote 2 down vote accepted

Like many others already correctly described, the ajax request runs asynchronous by default. So you need to deal with it in a proper way. What you can do, is to return the jXHR object which is also composed with jQuery promise maker. That could looke like

function getTags(level){
    return $.getJSON("php/get-tags.php", { "parent": level });
}

and then deal with it like

$(function(){
    getTags('0').done(function(json) {
        // do something with json
    });
});
share|improve this answer
add comment

You are trying to call an asynchronous function in a synchronous fashion.

When you call getTags, the it triggers a call to your PHP page, if javascript was blocking, then your page would hang until your server responded with the JSON. You need to re-think your logic and trigger a callback.

function getTags(level){
    $.getJSON("php/get-tags.php", { "parent": level }, function(json) {
        //do something with the JSON here.
    });
}
share|improve this answer
add comment

You cannot return from an AJAX call. It's asynchronous. You need to have all logic dealing with the returned data in the callback.

share|improve this answer
add comment

getJSON is asynchronous, the function that called it will have finished by the time the HTTP response gets back and triggers the callback.

You cannot return from it.

Use the callback to do whatever you want to do with the data.

share|improve this answer
add comment

If you need it to work like that, your getTagsfunction must return a value. It does not at the moment. You could achieve this by using $.ajax and setting async to false.

share|improve this answer
1  
async: false makes the entire browser lock up until the AJAX call is done. –  Rocket Hazmat Jan 11 '12 at 21:23
 
Umm. Yes. If that's what the OP wants..? –  bububaba Jan 11 '12 at 21:25
 
I meant that's a bad thing, as user may think their browsers have crashed. –  Rocket Hazmat Jan 11 '12 at 21:33
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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