0

好的,所以,我今天的问题是我试图从 AJAX 结果中返回一个 jquery 数据表,每次我请求 AJAX 时,我都会收到以下错误:

“DataTables 警告:无法解析来自服务器的 JSON 数据。这是由 JSON 格式错误引起的”

这是我的看法:

<script type="text/javascript">
        $(document).ready(function () {

            var oTable = $('#StudentTable').dataTable({
                "bServerSide": true,
                "sAjaxSource": "Admin/FindStudent",
                "bProcessing": true,
                "aoColumns": [
                    { "sName": "StudentID",
                        "bSearchable": false,
                        "bSortable": false,
                        "fnRender": function (oObj) {
                            return '<a href=\"Student/Details/' + oObj.aData[0] + '\">View</a>';
                        }
                    },
                    { "sName": "FirstName" },
                    { "sName": "LastName" },
                    { "sName": "EmailAddress" }
                ]
            });
        });
    </script>
<table id="StudentTable" class="display">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>First Name</th>
                        <th>Last Name</th>
                        <th>Email Address</th>
                    </tr>
                </thead>
                <tbody> 
                </tbody>
            </table>

这是控制器:

public ActionResult FindStudent(DataTableParamModel dtParams)
    {

        //if (Session["IsAdmin"] != "True")
        //{
        //    return View("Login");
        //}

        var repo = new StudentRepository();
        var allStudents = repo.GetAllStudents();
        IEnumerable<StudentInfo> filteredStudents;
        //Check whether the students should be filtered by keyword
        if (!string.IsNullOrEmpty(dtParams.sSearch))
        {
            //Used if particulare columns are filtered 
            var fnameFilter = Convert.ToString(Request["sSearch_1"]);
            var lnameFilter = Convert.ToString(Request["sSearch_2"]);
            var emailFilter = Convert.ToString(Request["sSearch_3"]);

            //Optionally check whether the columns are searchable at all 
            var isFNameSearchable = Convert.ToBoolean(Request["bSearchable_1"]);
            var isLNameSearchable = Convert.ToBoolean(Request["bSearchable_2"]);
            var isEmailSearchable = Convert.ToBoolean(Request["bSearchable_3"]);

            filteredStudents = repo.GetAllStudents()
               .Where(c => isFNameSearchable && c.FirstName.ToLower().Contains(dtParams.sSearch.ToLower())
                           ||
                           isLNameSearchable && c.LastName.ToLower().Contains(dtParams.sSearch.ToLower())
                           ||
                           isEmailSearchable && c.EmailAddress.ToLower().Contains(dtParams.sSearch.ToLower()));
        }
        else
        {
            filteredStudents = allStudents;
        }

        var isFNameSortable = Convert.ToBoolean(Request["bSortable_1"]);
        var isLNameSortable = Convert.ToBoolean(Request["bSortable_2"]);
        var sortColumnIndex = Convert.ToInt32(Request["iSortCol_0"]);
        Func<StudentInfo, string> orderingFunction = (c => sortColumnIndex == 1 && isFNameSortable ? c.FirstName :
                                                       sortColumnIndex == 2 && isLNameSortable ? c.LastName :
                                                       "");

        var sortDirection = Request["sSortDir_0"]; // asc or desc
        if (sortDirection == "asc")
            filteredStudents = filteredStudents.OrderBy(orderingFunction);
        else
            filteredStudents = filteredStudents.OrderByDescending(orderingFunction);

        var displayedStudents = filteredStudents.Skip(dtParams.iDisplayStart).Take(dtParams.iDisplayLength);
        var result = from c in displayedStudents select new[] { Convert.ToString(c.StudentID), c.FirstName, c.LastName, c.EmailAddress };
        JsonResult results = Json(new
                        {
                            sEcho = dtParams.sEcho,
                            iTotalRecords = allStudents.Count(),
                            iTotalDisplayRecords = filteredStudents.Count(),
                            aaData = result
                        },
                    JsonRequestBehavior.AllowGet);
        return View(results);
    }

使用 JSONLint 时,它会吐出

Parse error on line 1:
<headid="Head1"><sty
^
Expecting '{', '['

有任何想法吗?

4

1 回答 1

0

尝试返回 Json 对象:

return Json(new {
         sEcho = dtParams.sEcho,
         iTotalRecords = allStudents.Count(),
         iTotalDisplayRecords = filteredStudents.Count(),
         aaData = result
   }, JsonRequestBehavior.AllowGet);

资源

于 2012-09-14T16:17:03.340 回答