Mahesh,

If your object is already in JSON, there's really no need to use something
like Json.NET.  On the client, you could just parse out the JSON object
using eval statements or jQuery's JSON functions, but that's not a good
solution.

Something that popped out at me when I was rereading is that ASP.NET takes
the JSON in a string prior to consuming it.  What you're passing in is a
JSON object.  So, I'm wondering if ASP.NET isn't punting and producing an
error in your IIS logfiles.  Check this [1] website for more details.

[1]
http://weblogs.asp.net/sanjeevagarwal/archive/2008/07/29/Dynamically-create-ASP.NET-user-control-using-JQuery-and-JSON-enabled-Ajax-Web-Service.aspx

Good luck!
-- Dave

On Sat, Feb 28, 2009 at 8:20 AM, Mahesh <mrded...@gmail.com> wrote:

>
> 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.
>

Reply via email to