简体   繁体   English

jQuery,数据表和Ajax

[英]JQuery, Datatables and Ajax

I'm trying to dynamically load my datatable with an ASP.NET a web-method written in C#. 我试图用ASP.NET动态加载我的数据表,这是一种用C#编写的Web方法。 The method seems to work well but nothing I try gets the datatable to respond properly. 该方法似乎运行良好,但是我尝试的任何方法都无法使数据表正确响应。

Here's my code: 这是我的代码:

var oTable = $('table.datatable').dataTable({
  "processing": true,
  "serverSide": true,
  "ajax": {
    "url": "SearchForCustomers.aspx/GetUsers",
    "type": "POST",
    "contentType": "application/json; charset=utf-8",
    "dataType": "json"
  },
  "columns": [{
    "data": "A"
  }, {
    "data": "B"
  }, {
    "data": "C"
  }, {
    "data": "D"
  }, {
    "data": "E"
  }, {
    "data": "F"
  }]
});

My ASP.NET web-method: 我的ASP.NET网络方法:

public class AA
{
    public string A { get; set; }
    public string B { get; set; }
    public string C { get; set; }
    public string D { get; set; }
    public string E { get; set; }
    public string F { get; set; }
}

[WebMethod]
public static string GetUsers()
{
    /*List<UserAccount> listOfUserAccounts = UserAccount.GetUserAccounts(ApplicationConfiguration.ORDER_TYPES.DESC);
    JavaScriptSerializer jSearializer = new JavaScriptSerializer();
    return jSearializer.Serialize(listOfUserAccounts);*/

    List<AA> list = new List<AA>();
    list.Add(new AA { A = "a", B = "b", C = "c", D = "d", E = "e", F = "f" });
    list.Add(new AA { A = "a", B = "b", C = "c", D = "d", E = "e", F = "f" });
    list.Add(new AA { A = "a", B = "b", C = "c", D = "d", E = "e", F = "f" });
    list.Add(new AA { A = "a", B = "b", C = "c", D = "d", E = "e", F = "f" });
    list.Add(new AA { A = "a", B = "b", C = "c", D = "d", E = "e", F = "f" });
    list.Add(new AA { A = "a", B = "b", C = "c", D = "d", E = "e", F = "f" });
    list.Add(new AA { A = "a", B = "b", C = "c", D = "d", E = "e", F = "f" });

    JavaScriptSerializer jSearializer = new JavaScriptSerializer();
    return jSearializer.Serialize(list);
}

The AA class was made up in order to test the functionally of the datatable. 组成了AA类是为了测试数据表的功能。 The datatable receives no rows at all. 数据表根本不接收任何行。

Thanks in advance. 提前致谢。

Why it is not working with webmethod 为什么它不能与webmethod一起使用

Because, when Jquery datable makes a post request, it does send some parameter to post method , in this case it is GetUsers . 因为,当Jquery datable发出post请求时,它确实将一些参数发送到post method ,在本例中为GetUsers But since there is no input parameter for this function , it shows ajax error internal error 500 . 但是由于该function没有输入参数,因此它显示ajax错误internal error 500 Error message is - Invalid JSON primitive , if you search this, it says, if there is mismatch between jquery supplied param in post and webmethod ( in this case it is so), then this error occurs. 错误消息是- Invalid JSON primitive ,如果您搜索它,它说,如果post jquery提供的参数与webmethod之间不匹配(在这种情况下,则是这样),则发生此错误。 I know, this input parameter is not required at binding time, but still datatable ajax sends that for binding time, and those parameter are helpful for sorting , paging etc. Following is the screenshot,that shows POST params. 我知道,在绑定时不需要此输入参数,但仍然在绑定时将数据表datatable ajax发送该参数,并且这些参数对于sortingpaging等很有帮助。以下是截屏,显示了POST参数。

发布参数

How to solve this 如何解决这个问题

Move those methods to ashx hanlder , this way parameter ( such as sorting, searching etc..) will be captured by context.Request.Form ( this might not be required at binding time - this is your case). 将这些方法移至ashx hanlder ,这样,参数(例如排序,搜索等)将由context.Request.Form捕获(绑定时可能不需要(这是您的情况))。

But still, you need to modify supplied code, you need to wrap List<AA> to data and also include totalRecord , else jquery datatable shows error. 但是,仍然需要修改提供的代码,需要将List<AA>包装到data ,还需要包含totalRecord ,否则jquery datatable会显示错误。 Following is the code that i have tried and it is working. 以下是我尝试过的代码,它正在工作。

public void ProcessRequest(HttpContext context)
{
    context.Response.ContentType = "application/json";

    List<AA> list = new List<AA>();
    list.Add(new AA { A = "a", B = "b", C = "c", D = "d", E = "e", F = "f" });
    list.Add(new AA { A = "a", B = "b", C = "c", D = "d", E = "e", F = "f" });
    list.Add(new AA { A = "a", B = "b", C = "c", D = "d", E = "e", F = "f" });
    list.Add(new AA { A = "a", B = "b", C = "c", D = "d", E = "e", F = "f" });
    list.Add(new AA { A = "a", B = "b", C = "c", D = "d", E = "e", F = "f" });
    list.Add(new AA { A = "a", B = "b", C = "c", D = "d", E = "e", F = "f" });
    list.Add(new AA { A = "a", B = "b", C = "c", D = "d", E = "e", F = "f" });

    JavaScriptSerializer jSearializer = new JavaScriptSerializer();

    var result = new  { data = list, recordsTotal = 8 };

    context.Response.Write(jSearializer.Serialize(result));

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM