I am trying to create a dynamic web page which requires no post back and communicates with a server to get complex objects. I started out creating an AJAX enabled WCF Service, then writing some jquery to talk to the service. I got it working following this tutorial http://www.codeproject.com/Articles/132809/Calling-WCF-Services-using-jQuery.
I then realized I could use MVC a lone to do this, with the JsonResult type as the Action on a controller, following this tutorial: http://geekswithblogs.net/michelotti/archive/2008/06/28/mvc-json---jsonresult-and-jquery.aspx.
When I tried to leave the .cshtml page the way it was, and just plug in the different service (switching from WCF to MVC), my javascript started crashing (when it tries to evaluate response.length in the JS). I used the WebDevHelper tool to examine the Response Content of the JSON file and find a key difference.
The MVC JsonResponse gives me this result: ["anthony"]
The AJAX enabled WCF service gives me this: {"GetUserResult":["anthony"]}
The WCF method looks like this:
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]
string[] GetUser(string Id)
{ return new string[] { "foo", "bar" }; }
The MVC method looks like this:
public JsonResult GetUser(string Id)
{ return Json(new string[] { "foo", "bar" }); }
GetUser returns an array of strings (in this case it always returns a single item in the array).The method name on the WCF Service is called 'GetUser', but I don't understand fully what I'm looking at, I have no clue why the WCF would provide different data then the MVC, and lastly, why would the javascript work fine evaluating result.length, but crash because it cannot find a length property when using the smaller MVC data? It seems JSON isn't including the array information in the JsonResult?
thanks!