I have a ASP.NET web service which is returning a simple string value, I'm calling this web service through javascript using the script manager, everything is working fine, however, I need the return value from the location where I call the web service, and "not" from the callback function.
Something like this (sorry for the bad pseudo code)
function something() {
scriptmanager.webservice.method1(param, OnSuccess);
}
function OnSuccess(retVal) {
retVal <-- I need to do more with this, from within the "something" function above. Building an array for example calling this service multiple times.
}
I've tried creating a global javascript variable outside the functions, and assigning it in the OnSuccess function, but it always comes up undefined from the "something" function.
All of the examples out there usually change something visually on the page and don't really do something useful with the web service return value, how do I get the return value back up to the main calling "something" function?
You are describing a synchronous request instead of an asynchronous request, which the ASP.NET ScriptManager
does not support anything but asynchronous calls.
However, you can use the jQuery .ajax()
function to make a synchronous call, like this:
function something() {
var resultOfMethod1;
$.ajax({
type: "POST",
async: false,
url: "PageName.aspx/Method1",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(result) {
resultOfMethod1 = result.d;
}
});
// Do something here with resultOfMethod1
// resultOfMethod1 will have the result of the synchronous call
// because the previous $.ajax call waited for result before executing
// this line
}