1

May be, it is not so problematic for you. but i'm trying first time with json serialization. and also read other articles in stackowerflow.

I have created Entity Framework data model. then by method get all data from object:

private uqsEntities _db = new uqsEntities();
//get all data from table sysMainTableColumns where tableName=paramtableName
public List<sysMainTableColumns> getDataAboutMainTable(string tableName)
{
     return (from column in _db.sysMainTableColumns
                    where column.TableName==tableName
                    select column).ToList();

}

my webservice:

public string getDataAboutMainTable()
{
    penta.DAC.Tables dictTable = new penta.DAC.Tables();
    var result = dictTable.getDataAboutMainTable("1");
    return new JavaScriptSerializer().Serialize(result);
}

and jQuery ajax method

$('#loadData').click(function() {
            $.ajax({
                type: "POST",
                url: "WS/ConstructorWS.asmx/getDataAboutMainTable",
                data: "{}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(msg) {
                    $("#jsonResponse").html(msg);

                    var data = eval("(" + msg + ")");
                    //do something with data
                },
                error: function(msg) {

                }
            });
        });

Fails (from fairbug):

missing ] after element list [Break on this error] var data = eval("(" + msg + ")");

ajax Response (by Firebug if I remove var data = eval("(" + msg + ")")):

{"d":"[{\"ID\":1,\"TableName\":\"1\",\"Name\":\"d\",\"FullName\":\"f\",\"Type\":\"nvarchar(50)\",\"MeasurementUnit\":\"t         \",\"EntityState\":2,\"EntityKey\":{\"EntitySetName\":\"sysMainTableColumns\",\"EntityContainerName\":\"uqsEntities\",\"EntityKeyValues\":[{\"Key\":\"ID\",\"Value\":1}],\"IsTemporary\":false}},{\"ID\":2,\"TableName\":\"1\",\"Name\":\"e\",\"FullName\":\"e\",\"Type\":\"int\",\"MeasurementUnit\":\"r         \",\"EntityState\":2,\"EntityKey\":{\"EntitySetName\":\"sysMainTableColumns\",\"EntityContainerName\":\"uqsEntities\",\"EntityKeyValues\":[{\"Key\":\"ID\",\"Value\":2}],\"IsTemporary\":false}}]"}

problem with data, code fails there. and i think i'm not use JavaScriptSerializer().Serialize() method very well.

Please, tell me, what a big mistake I made in C# code?

9
  • Fails? Fails how? The eval is unnecessary, by the way. Commented Mar 15, 2010 at 19:50
  • @Craig-Stuntz: I think, now my question now is well formed. may be eval not unnecessary. but why ajax Responce is include tags like ":\"? Commented Mar 15, 2010 at 20:03
  • 1
    Looks to me like escaping the quote marks. BTW, it's a bad idea to serialize entities directly as JavaScriptSerializer will die if one happens to contain a circular reference. Commented Mar 15, 2010 at 20:12
  • 1
    I project onto an anonymous type. Change select column to select new { Id = column.Id, // etc. Commented Mar 15, 2010 at 20:53
  • 1
    $.each(msg.d, function ...) That ".d" is important! Commented Mar 15, 2010 at 22:06

2 Answers 2

3
  1. You don't need eval. jQuery does that for you when you specify dataType: "json"
  2. It's a bad idea to serialize entities directly as JavaScriptSerializer will die if one happens to contain a circular reference.
  3. Don't forget the d! That's inserted by WCF services to work around a security hole in some browsers when the root object is an array.
Sign up to request clarification or add additional context in comments.

2 Comments

Been wondering about that 'd'. Do know where I can find some more information about that?
It's a security feature since top-level arrays can be attacked in JavaScript when fetched with a GET. Same reason that return Json doesn't work in MVC 2 with a GET request unless you explicitly turn it on.
0

Have you tried debugging with Firebug or Fiddler to see what the JSON content looks like?

1 Comment

yes, sorry, i forget to publicate. please, whatch in question content

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.