I can’t seem to find any solution to this within SO so here goes.
I have a call to a WebMethod within the C# of my page like this;
$.ajax({
type: "POST",
url: "MyWebPage.aspx/jQueryMyWebMethod",
data: "{FamilyType:'" + $('.HdnFamilyType').val() + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
var obj = eval('(' + msg + ')');
}
});
In the code behind I create an object, serialise it and return it like this;
LHCRequiredFormViewModel fvm = new LHCRequiredFormViewModel();
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(LHCRequiredFormViewModel));
MemoryStream ms = new MemoryStream( );
ser.WriteObject(ms, fvm);
return Encoding.Default.GetString(ms.ToArray());
However, in the Javascript when I do the eval I get the error;
Expected ‘]’;
At the point of returning the serialised object from C# the data looks like;
"{\"<IsApplicantLHCRequired>k__BackingField\":true,\"<IsPartnerLHCRequired>k__BackingField\":false}" string
I should mention that the project was .Net 2.0 and I was simply returning a serialised object w/out all the JSON guff and it worked fine.
So I then converted to 3.5 and the problem began. It’s then that I used the JsonSerializer but am still getting exactly the same error.
Anyone know what’s going on and how to fix it?
A long time ago I had a problem similar to this. Unfortunately it was so long ago that I can’t remember how I ended up fixing it. I just know that the root cause was going from old version of .NET to 3.5.
If my memory serves me correctly try these:
Try using a different de-serializer on the client. Such as Json Parse from http://www.json.org/json_parse.js
If that still doesn’t work. Try a different serializer on the server. Json.NET is free and provides a lot of control over how you want to serialize your data: http://www.codeplex.com/Json
Or maybe I just needed to set the MimeType for the request to be text only instead of HTML.