无效的JSON权限绘制。在AJAX中使用jquery Datatables调用ASP.NET webforms中的Webmethod

时间:2017-02-23 13:35:26

标签: c# jquery asp.net ajax datatables

我正在尝试在我的ASP.NET webforms测试应用程序中为jquery Datatables.NET实现服务器端处理。

使用以下代码: Jquery:

$(document).ready(start);

function start(){
$('#PersonsTable').DataTable({
        "processing": true,
        "serverSide": true,
        "ajax": {
            "url": "/AjaxTest.aspx/GetPersonsHttp",
            "type" : "POST",
            "dataType": "json",
            "contentType": "application/json; charset=UTF-8"
        },
        "columns": [
            { "data": "FirstName" },
            { "data": "Name" }
        ]
    });
};

C#(在我的AjaxTest.aspx.cs中):

[WebMethod]
public static Person GetPersonsHttp()
{
    Person me = new Person() { FirstName = "John", Name = "Doe" };
    return me;
}

班主任:

public class Person
    {
        public string Name { get; set; }
        public string FirstName { get; set; }

        public override string ToString()
        {
            return string.Format("{0} {1}", this.FirstName, this.Name);
        }

    }

我的AjaxTest.aspx看起来像这样:

<table class="table table-hover table-striped" id="PersonsTable">
                <thead>
                    <tr>
                        <th>Vooraam</th>
                        <th>Naam</th>
                    </tr>
                </thead>
                <tbody>

                </tbody>
            </table>

最终我收到了以下错误:

{"Message":"Invalid JSON primitive: draw.","StackTrace":"   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject()\r\n   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)\r\n   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer)\r\n   at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)\r\n   at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)\r\n   at System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext context, JavaScriptSerializer serializer)\r\n   at System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext context)\r\n   at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.ArgumentException"}

有谁知道我在这里做错了什么?

1 个答案:

答案 0 :(得分:1)

尝试在静态方法之上添加[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]