views:

133

answers:

4

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: 

Although I'm not 100% sure, I believe the problem is in your serialized object.

    "{\"<IsApplicantLHCRequired>k__BackingField\":true,\"<IsPartnerLHCRequired>k__BackingField\":false}"

Probably the eval function is getting confused by the \" in there. Did you try changing that to ' ?

Seth Illgard
The serialized object is as is and is created by the serilization comment. I'm kinda hesitant it doing a replace given the command should give me propper serialized data. i suspect the problem is not in the backend but in the ui. i just don't think i have the javascript code right
griegs
A: 

First, I am not sure with C#

but could you try to change ms.ToArray() to ms like

return Encoding.Default.GetString(ms);
S.Mark
no sorry this does not work.
griegs
A: 

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.

Roberto Sebestyen
A: 

With jQuery.ajax, when you use json as the dataType, that's means "eval this for me because it's JSON" and the argument to your success callback will be an object. So you have this object that you're concatenating it with a string.

eval('(' + msg + ')');

That coerces msg to a string, and coercing an object to a string returns "[object Object]" for a simple hash like you have. So with the parenthesis you end up with:

"([object Object])"

Eval'ing that is a total syntax error.

As odge suggests, either drop the "eval" or drop the "json" dataType.

darkporter