我的数据表有问题。我收到错误:"无效的JSON原语:draw。"当我尝试从我的Web服务返回JSON时。很确定返回的JSON格式不正确,但我不确定如何纠正它。
使用Javascript:
$(document).ready(function () {
var dt = $('#example').DataTable({
processing: true,
serverSide: true,
pagingType: 'full_numbers',
ajax: {
url: 'http://localhost:49317/WebService2.asmx/GetTableData',
type: 'POST',
dataType: 'json',
contentType: 'application/json; charset=utf-8',
dataSrc: 'data'
},
columns: [
{ data: 0 },
{ data: 1 },
{ data: 2 },
{ data: 3 },
{ data: 4 }
],
order: [[1, 'asc']]
});
HTML:
<div>
<table id="example" class="table table-striped table-bordered" cellspacing="0" cellpadding="2" width="100%">
<thead>
<tr>
<th>Job</th>
<th>Customer</th>
<th>Planner</th>
<th>Description</th>
<th>SO</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
代码隐藏:
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;
using System.Data;
using System.Data.OleDb;
using Newtonsoft.Json;
using System.IO;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class WebService2 : System.Web.Services.WebService
{
public class JQDTParams
{
public string draw { get; set; }
}
[WebMethod(EnableSession = true)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet =false)]
public string GetTableData(JQDTParams param)
{
var sortOrder = "asc";
string connString = "Provider=Microsoft.ACE.OLEDB.12.0;data source=|DataDirectory|ProductionDataCopy.mdb";
DataTable results = new DataTable();
using (OleDbConnection conn = new OleDbConnection(connString))
{
OleDbCommand cmd = new OleDbCommand("SELECT * FROM ProductionData", conn);
conn.Open();
OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
adapter.Fill(results);
}
IEnumerable<DataRow> sequence = results.AsEnumerable();
var records = sequence.ToList();
var orderedResults = sortOrder == "asc"
? records.OrderBy(o => o["Job"])
: records.OrderBy(o => o["Job"]);
StringBuilder sb1 = new StringBuilder();
StringWriter sw = new StringWriter(sb1);
using (JsonWriter writer = new JsonTextWriter(sw))
{
writer.WriteStartObject();
writer.WritePropertyName("draw");
writer.WriteValue(param.draw.ToString());
writer.WritePropertyName("recordsTotal");
writer.WriteValue(records.Count);
writer.WritePropertyName("recordsFiltered");
writer.WriteValue(records.Count);
writer.WritePropertyName("data");
writer.WriteStartArray();
foreach (var result in orderedResults)
{
writer.WriteStartArray();
//writer.WriteValue("< img class='image-details' src='content/DataTables-1.10.4/images/details_open.png' runat='server' height='16' width='16' alt='View Details'/>");
writer.WriteValue(result["Job"]);
writer.WriteValue(result["Customer"]);
writer.WriteValue(result["Planner"]);
writer.WriteValue(result["Description"]);
//writer.WriteValue(result["so"]);
writer.WriteValue("so");
writer.WriteEndArray();
}
writer.WriteEndArray();
writer.WriteEndObject();
writer.Flush();
writer.Close();
}
sw.Close();
string Json = sb1.ToString();
//JavaScriptSerializer serializer = new JavaScriptSerializer();
//var Json = serializer.Serialize(sb1);
return Json;
}
}