Wednesday, March 28, 2012

Accessing Guid returned from WebService in Javascript

Hi, I have read through the forum posts and looked in the 'QuickStart' documentation but I am still stuck: How can one read the value of a System.Guid passed back to Javascript?

I have a WebService:
[WebMethod][XmlInclude (typeof(System.Guid))]
public Guid WebLogin(string UserID, string Password) {

Which I call, via Atlas, from JavaScript:
function LogIn(UserID, Pwd) {
K2.WebService.Connection.WebLogin(UserID, Pwd, LogInResult);
}

function LogInResult(result) {
debugger
alert (result);
} ...

I cannot access the value of 'result' (which will be a System.Guid). We used to use the old WebService.htc - where one could access 'result.value' and read the value as a string. I have tried adding the [XmlInclude typeof(System.Guid))], which a forum post hinted at, but nothing was added to the /js as a result.

Thanks in advance for any help you are able to provide.

kangaloosh.ps:

I have used the JSON.stringify() at www.json.org to look inside 'result' and it is empty. Have I got to create a converter class on the server side? If so is there a source code sample somewhere?

Thanks again.

I think a custom converter would be needed to make Guids work, since they don't have any properties that can be automatically serialized to Javascript. But the simplest solution is probably just to change your web method to return the Guid as a string.

How are you planning to use the Guid on the client?


Thanks for the speedy reply;

Will write a string based WebService wrapper for the existing WebMethod (as it is already in use from thick clients).

kangaloosh
Yes, that should work. Please let me know if it doesn't.

No comments:

Post a Comment