Jquery Ajax数据源(对象):TypeError:f未定义

Jquery Ajax数据源(对象):TypeError:f未定义,jquery,asp.net,json,datatables,webmethod,Jquery,Asp.net,Json,Datatables,Webmethod,我正在开发我的ASP.NETWeb应用程序,在这个应用程序中,我必须使用jQuery DataTables插件为一个HTML表填充Ajax数据源 HTML代码: <table class="table table-striped table-hover table-bordered display" id="example" cellspacing="0" width="100%"> <thead> <tr> &

我正在开发我的ASP.NETWeb应用程序,在这个应用程序中,我必须使用jQuery DataTables插件为一个HTML表填充Ajax数据源

HTML代码:

<table class="table table-striped table-hover table-bordered display" id="example" cellspacing="0" width="100%">
    <thead>
        <tr>
            <th>Prctice Group Risk No
            </th>
            <th>Practice_Group
            </th>
            <th>Risk_Category
            </th>
        </tr>
    </thead>
</table>
$('#example').DataTable({
    "ajax": {
        "dataType": 'json',
        "contentType": "application/json; charset=utf-8",
        "type": "POST",
        "url":"index.aspx/Risky"
    },
    "columns": [
        { "data": "Prctice_Group_Risk_No" },
        { "data": "Practice_Group" },
        { "data": "Risk_Category" }]
});
d:"[{"Prctice_Group_Risk_No":1,"Practice_Group":"M&A","Risk_Category":"Conflicts of Interests"},{"Prctice_Group_Risk_No":2,"Practice_Group":"abc","Risk_Category":"Client Care and Communication"}]
这是我调用的Web方法,以获取对象列表的JSON响应

 [WebMethod]
 [ScriptMethod]
    public static string Risky()
    {
        return JsonConvert.SerializeObject(riskList);
    }
来自服务器的JSON响应:

<table class="table table-striped table-hover table-bordered display" id="example" cellspacing="0" width="100%">
    <thead>
        <tr>
            <th>Prctice Group Risk No
            </th>
            <th>Practice_Group
            </th>
            <th>Risk_Category
            </th>
        </tr>
    </thead>
</table>
$('#example').DataTable({
    "ajax": {
        "dataType": 'json',
        "contentType": "application/json; charset=utf-8",
        "type": "POST",
        "url":"index.aspx/Risky"
    },
    "columns": [
        { "data": "Prctice_Group_Risk_No" },
        { "data": "Practice_Group" },
        { "data": "Risk_Category" }]
});
d:"[{"Prctice_Group_Risk_No":1,"Practice_Group":"M&A","Risk_Category":"Conflicts of Interests"},{"Prctice_Group_Risk_No":2,"Practice_Group":"abc","Risk_Category":"Client Care and Communication"}]
返回的JSON响应在我看来很好,如jquery DataTables官方站点中所述

但是表中没有填充任何数据,我在Firebug控制台中得到以下错误

TypeError:f未定义


默认情况下,jQueryDataTables需要以下格式的Ajax源数据

{ 
   "data": [

   ]
}
如果数据格式不同,则需要使用定义表数据的数据属性(
d

我不是ASP.NET专家,但您似乎用JSON格式对数据进行了两次编码

对于当前的服务器端代码,请尝试以下JavaScript代码:

$('#example').DataTable({
    "ajax": {
        "dataType": 'json',
        "contentType": "application/json; charset=utf-8",
        "type": "POST",
        "url":"index.aspx/Risky",
        "dataSrc": function (json) {
           return $.parseJSON(json.d);
        }
    },
    "columns": [
        { "data": "Prctice_Group_Risk_No" },
        { "data": "Practice_Group" },
        { "data": "Risk_Category" }
    ]
});

有关此错误和其他常见控制台错误的更多信息,请参阅。

Hello@Gyrocode.com能否请访问我的此问题的链接并帮助我@umer,刚刚添加了一个答案。已解决:请参阅此处的工作解决方案