• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 256
  • Last Modified:

javaqscript function gets the value but does not seem to return it - for reasons unknown!

The alert message in the function below shows the correct information:

 function GetUserID()
    {
        var theresult = "";

        $.ajax({
            type: "POST",
            url: "WebGlobalMethods.asmx/GetCurrentUserID",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: "{}",
            success: function (msg)
            {              
                theresult = msg.d;
                alert("web service returned:::  " + theresult);
            },
            error: function (msg)
            {
                alert("Possible web service failure:  " + msg.d);
            }
        });


        return theresult;
    }

Open in new window




but, back in the calling code.....it seems to disappear:


 var username = GetUserID();

        alert("user name passed back:  " + username);


this time it displays:

"user name passed back"

without the username tacked on the end of the message.

The username seems to be empty now, even though just moments before it had been returned successfully!


Any help? :(
0
Tom Knowlton
Asked:
Tom Knowlton
2 Solutions
 
Evan CutlerCommented:
your data call is inside a function.
you need to put the data output from the ajax query into something that will stay alive after the function.

Maybe a variable, or a form entity.
0
 
MacAnthonyCommented:
The problem is jquery's ajax call is asynchronous by default. Basically, the return theresult; is ran before the ajax call has finished so there is no value. You can make the call synchronous so it will wait until the ajax request has completed before moving on, but it might slow down the process. The other option is to change the success callback to handle whatever you need to do with theresult.

For synchronous execution:

 function GetUserID()
    {
        var theresult = "";

        $.ajax({
            type: "POST",
            async: false, // add this line
            url: "WebGlobalMethods.asmx/GetCurrentUserID",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: "{}",
            success: function (msg)
            {              
                theresult = msg.d;
                alert("web service returned:::  " + theresult);
            },
            error: function (msg)
            {
                alert("Possible web service failure:  " + msg.d);
            }
        });


        return theresult;
    }

Open in new window

0
 
Tom KnowltonWeb developerAuthor Commented:
async: false

did the trick, thank you!
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now