Monday, February 21, 2011

jQuery Ajax problem when modularized

I can't seem to make this work. I'm accessing this method from a seperate .js file.

function TabLoaderAJAX(xurl, xdata) {
    var result = null;
    $.ajax({
        type: 'POST',
        url: '/services/TabLoader.asmx/' + xurl,
        data: xdata,
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function(msg) {
            result = msg.d;
        }
    });

    return result;
}
From stackoverflow
  • Your request is asynchronous so when you return result variable it is set to null because the ajax request is not finished yet. You have to do a synchronous request by adding async:false in the option list. In this way you wait until the request finishes and then return the value of result.

    Martin Ongtangco : that clarifies things, thanks!

0 comments:

Post a Comment