Sunday, March 11, 2012

Passing JS object to webservice

Hi,

if you define a custom type in javascript and want to pass it to a webservice, you could serialize it from client to server, using XML or JSON or other data-interchange format.

In my opinion, a better alternative is to serialize the object from server to client, using the JSON serializer and proxy builder of the Atlas framework, and define your custom type in the WebService .asmx file. When you add a reference to the .asmx file in the ScriptManager, types defined in the .asmx file are serialized, thus you can instantiate them at client side.

For example, if you declare the following class in a MyWebService.asmx file:

public class Member
{
private string name = String.Empty;
private string lastName = String.Empty;

public string Name
{
get { return name; }
set { name = value; }
}

public string LastName
{
get { return lastName; }
set { lastName = value; }
}
}

and add a reference to MyWebService in the ScriptManager, you get a proxy for the Member type, and can instantiate it:

var m = new Member();
m.Name = "John";
m.LastName = "Doe";
alert(m.Name + ' ' + m.LastName);

You can also use WebMethods to pass or return Member instances.

Just a small clarification on what Garbin wrote: in your case, you are not looking to serialize the object from server to client, but from client to server. What the server send to the client is Javascript proxy code that allow you to create an object of the server type (e.g. Member) on the client, and pass it to a server side method as a single object.

David


Hi,

oops, sorry for the confusion I made with serialization/proxying. As davidebb remarked, my suggestion was to proxy on client side a type defined at server side (in a web service) and then instantiate and serialize it to the server. This allows to accomplish the task that EdwardC asked, using the Atlas framework's features.
Thanks for the clarification David.

Many thanks to you all for prompt replies. I am a newbie where serialization and proxying are still an iUnknown to me. Can someone please put up some sample code that demostrate the power of Atlas on the client side ?

My design goal is NOT to confuse the developer with server side scripting. As server scripts will be designed to generically take care all backend task. Client will only pass information and do nothing else. Server web service is like a black box to them. Suggestion welcome.


2 EdwardC: these nice samples demonstrate either client-centric model or server-centric model of ASP .NET Atlas applications:


http://www.nikhilk.net/Content/Presentations/TechReadySamples.zip


If you have some other samples for Atlas December CTP, let me to know, please.


These samples demonstrate either client-centric model or server-centric model of developing ASP .NET Atlas applications:

http://www.nikhilk.net/Content/Presentations/TechReadySamples.zip

If you have some other samples for December CTP, let me know, please.


RE: Calling .net 1.1 remote domain web service with JavaScript / JSON

I was following this thread because I am attempting to construct an .NET 1.1 web service that returns JSON, I need this for cross domain web service access. I have an example of the client js that calls Yahoo http://v026u20eww.maximumasp.com/wsjson/jsontest.htm and uses the js script athttp://v026u20eww.maximumasp.com/wsjson/jsr_class.js but I do not know how to make a .net 1.1 web service that could be accessed with and return values to this script and page. Any and all advise welcome! Thank you in advance.

BTW the example link and script above are fromhttp://www.xml.com/pub/a/2005/12/21/json-dynamic-script-tag.html?page=1


ttdbn -

This is exactly what I would like to do to allow for simple cross domain support. It seems there are many sites that describe how to consume JSON, but few that show how to modify a .NET "webmethod" so that it outputs JSON instead of SOAP. Have you had any luck with this yet? This seems like it would have been addressed since xml can not be transfered across domains with modifying browser security settings or creating a Proxy.

Ben


Just to follow up here is an interesting comment from Dan Theurer in response to a question. It sounds like Dan works on this for Yahoo.

http://www.theurer.cc/blog/blog/2005/12/15/web-services-json-dump-your-proxy/#comment-233

Still clear as mud for me.

Ben

No comments:

Post a Comment