views:

543

answers:

1

My mvc page returns json by using the function Json(mycustomclass) to return a JsonResult object.

This works just fine except, I need to wrap the json in a callback so that jQuery can use it.

I want it like this:
jsonp1246168411282({"myjson":"some values"})

but I am getting this: {"myjson":"some values"}

Is there any way I can 'wrap' the C# JsonResult with brackets and the jquery callback?

Thanks

The Lorax

+1  A: 

Jquery can use the json response just fine unless you are calling this action from a page in another domain. Is this what you are doing? If so I advise you to create a wcf service which supports jsonp. Example here

If not you can just use the getJson jquery method.

redsquare
Hi redsquare. Yes, my service needs to be accessible from other domains. Currently I have I view page that returns the json but I am working on a better solution that allows me to serialize a large custom class into json and return it as an object. I might just use an open source json serializer to turn it into a string and return it as a string.Thanks for your help thought.Best Regards,The Lorax
The_Lorax
good idea, I use this lib http://james.newtonking.com/projects/json-net.aspx
redsquare