Hi Dave, I'm using .NET 2.0. And the web method is working fine with static parameters. I could find a temporary solution of passing the JSON object as a string and parsing the string into my .NET object using an open source library Json.NET. This way it works, but I'd be glad to avoid the 2 additional steps that I have to carry out.
Regards, Mahesh Dedhia. On Feb 27, 7:25 pm, David Meiser <dmei...@gmail.com> wrote: > Mahesh, > > A few things, while I'm remembering: > 1) Are you using .NET 3.5? The method I gave you will (probably) only work > with 3.5, although it might work with 3.0. > > 2) Make sure you have the following before the class in your ASMX or ASPX > file: [System.Web.Script.Services.ScriptService] (I believe this is new to > 3.5, and is necessary to call your WebService via javascript) > > Does your web method return data if you pass in static data (eg - data: > "{'fname':'John','lname':'Doe'}" in your data declaration on the JQuery > side)? > > On Fri, Feb 27, 2009 at 7:05 AM, Mahesh <mrded...@gmail.com> wrote: > > > Hey Dave, > > Yes, I'm using ASP.NET web method inside an ASPX page. > > I tried your solution, but didn't work. > > Here, the code that I used. > > > Default.js > > function Call() { > > > var person = { "fname" : "John", > > "lname" : "Doe"}; > > > $.ajax({ > > type: "POST", > > url: "Default.aspx/WebMethod", > > //data: {}, > > data: person, > > contentType: "application/json; charset=utf-8", > > dataType: "json", > > success: function(msg) { > > alert('Successful'); > > } > > }); > > }; > > > Default.aspx.cs > > [WebMethod] > > public static object WebMethod(string fname, string lname) > > { > > return new { fname = fname, lname = lname }; > > } > > > Didn't work.. > > Apart from that, isn't there a way to recognize the JSON object as > > Object and type-cast it to a similar .NET object? > > I found something here, > > >http://www.dennydotnet.com/post/2008/03/03/Passing-a-JSON-object-to-a... > > But it is using WCF which I don't want to. > > > Regards, > > Mahesh. > > > On Feb 26, 11:00 pm, David Meiser <dmei...@gmail.com> wrote: > > > Are you talking about an ASP.NET web method? > > > > If you are, I think that you'll find it pretty easy using this: > > > > $.ajax({ > > > type: "POST", > > > contentType: "application/json; charset=utf-8", > > > url: "myWebService.asmx/myWebMethod", > > > data: myJSONObject > > > dataType: "json", > > > success: function(json) { // do Stuff } > > > }); > > > > Just make sure that myWebMethod takes each item in the JSON object as a > > > parameter and everything should work pretty well. > > > > Good luck! > > > - Dave > > > > On Thu, Feb 26, 2009 at 9:45 AM, Mahesh <mrded...@gmail.com> wrote: > > > > > Hello, > > > > How do I pass a JSON object to a server side web method as a > > > > parameter? > > > > > For example: > > > > I have the following JSON object. > > > > var person = { "firstName" : "John", > > > > "lastName" : "Doe", > > > > "age" : 23 }; > > > > > and I want to pass this object in the data: "{}" block. > > > > Is it possible? > > > > > Regards, > > > > Mahesh.