What if you had a web service method like this:
[WebMethod]
public List<String> Test() {
List<String> blah = new List<string>();
blah.Add("First");
blah.Add("Second");
return blah;
}
The method returns a generic List<> type of String objects. What happens if you want to consume it from AJAX via a web service method call like this?
function callWebService()
{
WebService.Test(OnMethodSucceeded,OnMethodFailed);
return;
}
The result will get sent back to the browser as an array type in JavaScript. Doing an alert() call on the result returned from OnMethodSucceeded like this:
function OnMethodSucceeded(result, eventArgs)
{
alert(result);
}
Yields the string "First,Second". You may also index into the array too and get the individual strings.
So, what about parameters? ECMA script doesn't support generics in the same way .NET does, but given that we've identified the runtime returns the generic list as an array back to ECMA script, it seems logical that we could pass a "generic list" to the runtime via ECMA script as an array. Indeed, we can. If we change the web service method to something like this:
[WebMethod]
public String Test(List<String> arg) {
if (arg.Count > 0)
return arg[0];
else
return "NA";
}
We can call it from script like this:
function callWebService()
{
var arr= new Array(5)
arr[0]="1";
arr[1]="2";
arr[2]="3";
arr[3]="4";
arr[4]="5";
WebService.Test(arr,OnMethodSucceeded,OnMethodFailed);
return;
}
The web service method returns, as a string, the first item in the array. OnMethodSucceeded, with our alert() method, will display "1" returned to the client.