Jquery数据表与服务器端使用asmx
本文关键字:asmx 服务器端 数据表 Jquery | 更新日期: 2023-09-27 18:16:31
我使用Jquery Datatable 1.10.12使用服务器端处理选项。我尝试了代码从他们的网站JQuery数据表服务器端,但没有运气。我做错了哪一页没有显示记录,并给出错误500任何帮助将是伟大的。
我代码:。aspxpage
<script>
$(document).ready(function () {
$('#example').DataTable({
serverSide: true,
ajax: 'EmployeeService.asmx/GetPersons'
});
});
</script>
<body>
<table id="example" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th>BusinessEntityID</th>
<th>FirstName</th>
<th>MiddleName</th>
<th>LastName</th>
<th>PersonType</th>
</tr>
</thead>
</table>
</body>
asmx:
[WebMethod]
public void GetPersons(int iDisplayLength, int iDisplayStart, int iSortCol_0, string sSortDir_0, string sSearch )
{
int displayLength = iDisplayLength;
int displayStart = iDisplayStart;
int sortCol = iSortCol_0;
string sortDir = sSortDir_0;
string search = sSearch;
List<Person> pdetails = new List<Person>();
int filteredcount = 0;
using (SqlConnection con = new SqlConnection("Data Source=xxx;Initial Catalog=AdventureWorks;Integrated Security=True"))
{
SqlCommand cmd = new SqlCommand(("spGetPersons"), con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter pDisplayLength = new SqlParameter()
{
ParameterName = "@DisplayLength",
Value = displayLength
};
cmd.Parameters.Add(pDisplayLength);
SqlParameter pDisplayStart = new SqlParameter()
{
ParameterName = "@DisplayStart",
Value = displayStart
};
cmd.Parameters.Add(pDisplayStart);
SqlParameter pSortCol = new SqlParameter()
{
ParameterName = "@SortCol",
Value = sortCol
};
cmd.Parameters.Add(pSortCol);
SqlParameter pSortDir = new SqlParameter()
{
ParameterName = "@SortDir",
Value = sortDir
};
cmd.Parameters.Add(pSortDir);
SqlParameter pSearchString = new SqlParameter()
{
ParameterName = "@Search",
Value = string.IsNullOrEmpty(search)?null:search
};
cmd.Parameters.Add(pSearchString);
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Person per = new Person();
per.BusinessEntityID = rdr["BusinessEntityID"].ToString();
filteredcount = Convert.ToInt32(rdr["TotalCount"].ToString());
per.FirstName = rdr["FirstName"].ToString();
per.MiddleName = rdr["MiddleName"].ToString();
per.LastName = rdr["LastName"].ToString();
per.PersonType = rdr["PersonType"].ToString();
pdetails.Add(per);
}
}
var result = new
{
draw = 1,
recordsTotal = GetPersonsTotalCount(),
recordsFiltered = filteredcount,
data = pdetails
};
JavaScriptSerializer js = new JavaScriptSerializer();
js.MaxJsonLength = 2147483644;
Context.Response.Write(js.Serialize(result));
}
private int GetPersonsTotalCount()
{
int totalPersonCount = 0;
using (SqlConnection con = new SqlConnection("Data Source=xxx;Initial Catalog=AdventureWorks;Integrated Security=True"))
{
SqlCommand cmd = new SqlCommand(("select count(*) from Person.Person"), con);
con.Open();
totalPersonCount = (int)cmd.ExecuteScalar();
}
return totalPersonCount;
}
public class Person
{
public string BusinessEntityID { get; set; }
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public string PersonType { get; set; }
}
JSON :
{"draw":1,"recordsTotal":19972,"recordsFiltered":19972,"data":[{"BusinessEntityID":"1","FirstName":"Ken","MiddleName":"J","LastName":"Sánchez","PersonType":"EM"},{"BusinessEntityID":"2","FirstName":"Terri","MiddleName":"Lee","LastName":"Duffy","PersonType":"EM"},{"BusinessEntityID":"3","FirstName":"Roberto","MiddleName":"","LastName":"Tamburello","PersonType":"EM"},{"BusinessEntityID":"4","FirstName":"Rob","MiddleName":"","LastName":"Walters","PersonType":"EM"},{"BusinessEntityID":"5","FirstName":"Gail","MiddleName":"A","LastName":"Erickson","PersonType":"EM"},{"BusinessEntityID":"6","FirstName":"Jossef","MiddleName":"H","LastName":"Goldberg","PersonType":"EM"},{"BusinessEntityID":"7","FirstName":"Dylan","MiddleName":"A","LastName":"Miller","PersonType":"EM"},{"BusinessEntityID":"8","FirstName":"Diane","MiddleName":"L","LastName":"Margheim","PersonType":"EM"},{"BusinessEntityID":"9","FirstName":"Gigi","MiddleName":"N","LastName":"Matthew","PersonType":"EM"},{"BusinessEntityID":"10","FirstName":"Michael","MiddleName":"","LastName":"Raheem","PersonType":"EM"}]}
错误:http://localhost: 64400/EmployeeService.asmx/getperson ?画= 1,列% 5 b0 % 5…艺术= 0,长度= 10,搜索% 5 bvalue % 5 d =,搜索% 5 bregex % 5 d = false& _ = 1478471333896加载资源失败:服务器响应状态为500。处理步骤(Internal Server Error) Server Error in '/' Application.
请求格式无法识别,URL意外以。结尾/getperson。
描述:在执行过程中发生未处理的异常当前的web请求。请查看堆栈跟踪了解更多信息有关错误及其在代码中的起源的信息。
Exception Details: System。InvalidOperationException:请求格式为URL意外以'/GetPersons'结尾,无法识别
源错误:
的过程中产生了一个未处理的异常当前的web请求。关于来源和地点的信息可以使用下面的异常堆栈跟踪来识别异常。堆栈跟踪:
[InvalidOperationException: URL的请求格式无法识别。[/GetPersons]
System.Web.Services.Protocols.WebServiceHandlerFactory.CoreGetHandler(类型HttpRequest请求,HttpResponse响应)+ 169669 System.Web.Services.Protocols.WebServiceHandlerFactory.GetHandler (HttpContext上下文,字符串动词,字符串url,字符串filePath) +209
System.Web.Script.Services.ScriptHandlerFactory.GetHandler (HttpContext上下文,字符串请求类型,字符串url,字符串路径翻译)+120
System.Web.MaterializeHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute ()+ 339 System.Web.HttpApplication。ExecuteStep(IExecutionStep step, Boolean&completedSynchronously) + 155
请像
那样修改您的Asmx文件 [WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
[ScriptMethod(UseHttpGet=true)]
public void GetPersons(int iDisplayLength, int iDisplayStart, int iSortCol_0, string sSortDir_0, string sSearch )
{
}
修改web配置,在web服务中添加协议
<system.web>
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>