首页 > 解决方案 > 如何将参数传递给 AJAX 以加载 Datatable?

问题描述

我正在绑定 Jquery DataTable。为此,我将从文本框中获取参数值并发送到服务器,然后返回获取 json 并填充 DatTable

我为此参考此链接: Pass_Parameter_To_Jquery_DataTable

但它的错误如

Uncaught Sys.ParameterCountException: Sys.ParameterCountException: Parameter count mismatch.

控制台错误

在此处输入图像描述

jQuery 数据表

function LoadTableData() {
            $.ajax({
                url: 'UserService.asmx/GetData',
                method: 'post',
                dataType: 'json',
                data: function (d) {
                    d.UserName = $('#txtUserName').val();
                    d.UserID = $('#txtUserID').val();
                    d.Status = $('input[name="Status"]:checked').val();
                },
                success: function (data) {
                    $('#example').dataTable({
                        "processing": true,
                        "serverSide": true,
                        data: data,
                        columns: [
                            {
                                render: function (data, type, row, meta) {
                                    return meta.row + meta.settings._iDisplayStart + 1;
                                }
                            },
                            { 'data': 'Status', class: 'editable text' },
                            { 'data': 'USER_LOGIN', class: 'editable text' },
                            { 'data': 'USER_NAME', class: 'editable text' },
                            {
                                //edit button creation    
                                render: function (data, type, row) {
                                    return createButton('edit', row.id);
                                }
                            },
                            {
                                //delete button creation    
                                render: function (data, type, row) {
                                    return createButton('delete', row.id);
                                }
                            }
                        ],
                        dom: 'Bfrtip',

                        buttons: [
                            'copy', 'csv', 'excel', 'pdf'
                        ],
                        "searching": true,
                        "paging": true,
                        "info": true,
                        "language": {
                            "emptyTable": "No data available"
                        },
                     })
                   }
                }
            });
        };

服务器网络方法

 public class StoreJSON
    {
        public string UserName { set; get; }
        public string UserID { set; get; }
        public string Status { set; get; }

    }

    [WebMethod]
    public void GetData(StoreJSON model)
    {

        string UserIDModify = "";
        string UserNameModify = "";
        string PasswordModify = "";
        int StatusModify;

        if (model.UserID != "")
            UserIDModify = model.UserID;
        else
            UserIDModify = "%";
        if (model.UserName != "")
            UserNameModify = model.UserName;
        else
            UserNameModify = "%";
        PasswordModify = "%";
        if (model.Status.ToString() == ("Active"))
            StatusModify = 1;
        else
            if (model.Status.ToString() == ("Inactive"))
            StatusModify = 0;
        else
            StatusModify = 0;

        DataTable dt_MobileUserLogin = FAV_VS_BLL.Search_MobileUserLogin(UserNameModify, UserIDModify, PasswordModify, StatusModify);
        List<MobileUserMaster> list = new List<MobileUserMaster>();

        foreach (DataRow dr in dt_MobileUserLogin.Rows)
        {
            list.Add(new MobileUserMaster
            {
                USER_LOGIN = dr["USER_LOGIN"].ToString(),
                USER_NAME = dr["USER_NAME"].ToString(),
                Status = dr["status"].ToString(),
                PASSWORD = dr["PASSWORD"].ToString()
            });
        }
        JavaScriptSerializer jss = new JavaScriptSerializer();
        Context.Response.Write(jss.Serialize(list));
    }

我在哪里做错了请帮助我

标签: jqueryajax

解决方案


发生这种情况是因为库(DataTables)需要由服务器发送的已定义响应 json,在我的情况下,我在 java 中使用此模型:

public class RespuestaResultados<E> {
  private int draw;
  private long recordsTotal;
  private long recordsFiltered;
  private List<E> data;
  private Object infomation;

  public RespuestaResultados() {
    super();
  }

  public int getDraw() {
    return draw;
  }

  public void setDraw(int draw) {
    this.draw = draw;
  }

  public long getRecordsTotal() {
    return recordsTotal;
  }

  public void setRecordsTotal(long recordsTotal) {
    this.recordsTotal = recordsTotal;
  }

  public long getRecordsFiltered() {
    return recordsFiltered;
  }

  public void setRecordsFiltered(long recordsFiltered) {
    this.recordsFiltered = recordsFiltered;
  }

  public List<E> getData() {
    return data;
  }

  public void setData(List<E> data) {
    this.data = data;
  }

  public Object getInfomation() {
    return infomation;
  }

  public void setInfomation(Object infomation) {
    this.infomation = infomation;
  }

}

推荐阅读