将Json加载到datatable

时间:2013-11-17 21:05:17

标签: c# jquery ajax json datatables

我做错了什么? 这是我的代码:

           $.ajax({
            type: "POST",
            url: "GetData.asmx/GetEventMembers",
            data: "{'ShulID': '" + ShulID
               + "','EventID': '" + EventID + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) 
                     {
                        $('#tblEventMembers').dataTable({                        
                            "aaData": eval(data.d)
                        });
                     },
            failure: function (msg) {
                alert(msg);
            }
        });

这是我的json:

{
  "aaData": [
{
  "MemberID": 22,
  "FName": "hfhfh",
  "LName": "fhfhfh",
  "InvitationDate": null,
  "Approved": false,
  "Invited": 0
},
{
  "MemberID": 42,
  "FName": "fkfk",
  "LName": "vm.,v",
  "InvitationDate": null,
  "Approved": false,
  "Invited": 0
}
  ]}

我收到了这个错误:

enter image description here

有什么想法吗?

谢谢。

2 个答案:

答案 0 :(得分:0)

ajax data对象必须是键/值对。如果value是一个数组,jQuery会根据值

使用相同的键序列化多个值

所以尝试改变这个:

 data: "{'ShulID': '" + ShulID
               + "','EventID': '" + EventID + "'}",

到此:

data : {
 shulID : ShulID,
 EventID : EventID
},

答案 1 :(得分:0)

最后我解决了。 我的问题是我按原样使用了json响应。 现在我添加了$.parseJSON()并且它运行良好。 我的代码现在看起来像这样:

            $.ajax({
            type: "POST",
            url: "GetData.asmx/GetEventMembers",
            //data: {ShulID: iShulID, EventID: iEventID},
            data: "{'ShulID': '" + iShulID
            + "','EventID': '" + iEventID + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            //sAjaxDataProp: "",
            success: function (jsonRes) 
            {

                var oTable = $('#tblEventMembers').dataTable({
                    "bDestroy": true,
                    "aaData": $.parseJSON(jsonRes.d).aaData,
                    "aoColumns": [
                                    { "mData": "MemberID" },
                                    { "mData": "FName" },
                                    { "mData": "LName" },
                                    { "mData": "InvitationDate" },
                                    { "mData": "Approved" },
                                    { "mData": "Invited" }
                                 ]
                });
            },
            failure: function (msg) {
                alert(msg);
            }
        });