The problem with your code seems to be related with async operation delay. When you call
function1()
and expect to get the result in
var1
then you will get the result only after the successful completion of your web service call in
function1()
.
However your
alert()
is being called before any data is actually returned from the async call and hence you get undefined error.
So the solution is to call the
alert()
when your async operation has been completed successfully. For example use jQuery to call your web service and assign success and failure callbacks appropriately:
$.ajax({
type: "POST",
url: 'Web Service Url',
data: JSON.stringify(Data),
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (result) { alert("show username :" + result.UserName); },
error: function (e) { }
});
I hope you are understanding this correctly. :)