let’s say i have a class:
[Serializable]
public sealed class MyFoo
{
public int ID { get; set; }
public string Name { get; set; }
}
I want to pass data from my Webservice to the JQuery ajax this class to the JS and parse it like an object.
[WebMethod]
public MyFoo GetData()
{
return (new MyFoo);
}
$.ajax({
success: function(val) {
var MyFoo = val;
$('#textbox1').val(MyFoo.ID);
$('#textbox2').val(MyFoo.Name);
}
});
It depends what framework you’re using but there are lots of ways to achieve this. For example, in MVC you can return a
JsonResultthat contains any object, and it will be serialised to JSON in a straightforward way.Given the
[WebMethod]attribute I guess you’re using Microsoft’s Web Services framework and I think it will accept and return JSON if that’s what you request in yourajaxcall.Here’s a blog post on this subject.