Search code examples
javascriptweb-servicesasp.net-ajaxwebmethod

ASP.NET + AJAX + WebService: string result + out parameter


In my experience I call simple web methods, like this:

[WebMethod]
public List<string> GetUserListByLetters(string strLetters){ ... }

And here is my OnComplete JS-function:

function OnComplete(args) {
    ...
    if (args != "") {
        for (var i = 0; i < args.length; i++) {
            // Do what I need with string in args[i]
        }
    }
    ...
}

For now I would like to have such method:

[WebMethod]
public string GetUserListByCountry(int countryId, out List<User> users)
{ 
    users=null;
    if ( Validate(countryId)==false )
        return "wrong country Id";
    users = GetUsers(countryId); // returns list of User objects.
    return "";
}

Question1: should "out" parameter work in WS? I saw few article (, for example) where said it is impossible. Question2: if it doesn't work, how should I change method signature to get that workable? Question3: if it works, how could I access data from 'out' parameter?

Thanks.


Solution

  • I will use this approach:

    [WebMethod]
    public object GetUserListByCountry(int countryId)
    { 
        users=null;
        if ( Validate(countryId)==false )
            return "wrong country Id";
        users = GetUsers(countryId); // returns list of User objects.
        return new {Error="", Users=users};
    }
    

    I mean that I will return complex object, its one property will contain usual value for return and another - required data.