0
function vote_helper(content_id, thevote){
            var result = "";
            $.ajax({ 
                type:"POST",
                url:"/vote",
                data:{'thevote':thevote, 'content_id':content_id},
                beforeSend:function() {
                },
                success:function(html){
                       result = html;
                }
            });
            return result;
        };

I want to return the result. But it's returning blank string.

4
  • Return? From the function vote_helper? You can't, the POST hasn't completed yet. Commented Jan 11, 2011 at 0:46
  • 7
    Oh dear, another "how do I return from an asynchronous function" question. Commented Jan 11, 2011 at 0:48
  • 1
    @Jacob, it's fair enough though. Without knowing that the problem is even to do with asynchronous, it's hard to know what to search for. Commented Jan 11, 2011 at 0:50
  • 1
    @Jacob: I've responded to at least 3 questions like this also, it is a bit frustrating. @Box9 If you're using AJAX, it would behoove you to at least research something about asynchronous programming since that's what the A in AJAX means. Commented Jan 11, 2011 at 0:56

4 Answers 4

2

Short answer, you can't.

Long answer, .ajax uses a callback to return the value. This means that the value may or may not have been returned already by the time the return fires. But either way, it's being done so in another context.

If you're looking to make this simulate returning a value, add a new argument to your function that will replace the ajax callback. Something such as:

 function vote_helper(content_id, thevote, callback){
    var result = "";
    $.ajax({ 
        type:"POST",
        url:"/vote",
        data:{'thevote':thevote, 'content_id':content_id},
        beforeSend:function() {
        },
        success:callback
    });
    return result;
};

vote_helper(x,y,function(html){
  result = html;
});

But work-around or not, the reply will never be in the same working path as the code that calls the function. You need to await the response and pick up processing from there.

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

1 Comment

Keep in mind that the result in your callback won't be able to access the result variable in the vote_helper function unless you declare it outside the vote_helper. As it is, you'll be creating a global variable.
1

Since you're making an AJAX call, you need to process the result of the AJAX call in the success callback:

function vote_helper(content_id, thevote){
            $.ajax({ 
                type:"POST",
                url:"/vote",
                data:{'thevote':thevote, 'content_id':content_id},
                beforeSend:function() {
                },
                success:function(html){
                /* Do something like call a function with html */
                }
            });
        };

Comments

1

The ajax won't complete by the time your function ends, so you can't return a result. Instead you have to modify your function to accept a callback, and call that callback with the result:

function vote_helper(content_id, thevote, callback) { // extra callback argument
    $.ajax({ 
        type: "POST",
        url: "/vote",
        data: {'thevote':thevote, 'content_id':content_id},
        beforeSend: function() {},
        // Point directly to the callback here
        success: callback
    });
};

Comments

0

If you want the UI to be completely unresponsive while waiting for the server's response, you could do the following:

function vote_helper(content_id, thevote){
        var result = "";
        $.ajax({ 
            type:"POST",
            async: false, //This line will make your code work
            url:"/vote",
            data:{'thevote':thevote, 'content_id':content_id},
            beforeSend:function() {
            },
            success:function(html){
                   result = html;
            }
        });
        return result;
};

But nobody wants the UI to hang, so the real answer is what other suggested, instead of returning a value, your method should take a callback that will be passed the 'return value' when your asynchronous method returns.

Comments

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.