C# Simple Ajax Example Gets JSON Formatting Error
C# Simple Ajax Example Gets JSON Formatting Error
jgelinas33
Posts: 0Questions: 0Answers: 0
This is the most basic example that I can think of. I'm manually creating the JSON in a string to pass back to data tables call. The JSON returned certainly looks to be well formed, but DataTables is throwing the error: 'DatatTables warning (table id='table_id'): DataTables warning: JSON data from server could not be parsed. This is caused by a JSON formatting error.'
The JSON returned in the response, captured in Firefox/Firebug is as follows:
{"sEcho": 1,"iTotalRecords": "2","iTotalDisplayRecords": "2","aaData":[["Vendor1","10","20","30","50"],["Vendor1","10","20","30","50"]]}
This seems to be a valid JSON string as per the DataTables documentation.
Can anyone tell me what is going wrong here?
The HTML Page:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
@import"/Styles/demo_table.css";
@import"/Sytels/demo_page.css";
var oTable;
$(document).ready
(
function () {
$("#table_id").dataTable({
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "WebForm1.aspx"
});
});
Vendor
Past 7 Days
Past 24 Hours
Past Hour
Rejects Outstanding
>
The ASPX Code Behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Web;
using System.Web.UI;
using System.Data;
using System.Data.SqlClient;
using System.IO;
namespace DataTablesInWebApplication
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, System.EventArgs e)
{
string dtJSONString;
dtJSONString = @"{";
dtJSONString = dtJSONString + @"""sEcho"": " + Request["sEcho"] + ",";
dtJSONString = dtJSONString + @"""iTotalRecords"": ""2" + @""",";
dtJSONString = dtJSONString + @"""iTotalDisplayRecords"": ""2" + @""",";
dtJSONString = dtJSONString + @"""aaData"":[";
dtJSONString = dtJSONString + "[" + @"""Vendor1""," + @"""10""," +
@"""20""," + @"""30""," + @"""50""],";
dtJSONString = dtJSONString + "[" + @"""Vendor1""," + @"""10""," +
@"""20""," + @"""30""," + @"""50""]";
dtJSONString = dtJSONString + "]}";
HttpContext.Current.Response.Write(dtJSONString);
}
The JSON returned in the response, captured in Firefox/Firebug is as follows:
{"sEcho": 1,"iTotalRecords": "2","iTotalDisplayRecords": "2","aaData":[["Vendor1","10","20","30","50"],["Vendor1","10","20","30","50"]]}
This seems to be a valid JSON string as per the DataTables documentation.
Can anyone tell me what is going wrong here?
The HTML Page:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
@import"/Styles/demo_table.css";
@import"/Sytels/demo_page.css";
var oTable;
$(document).ready
(
function () {
$("#table_id").dataTable({
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "WebForm1.aspx"
});
});
Vendor
Past 7 Days
Past 24 Hours
Past Hour
Rejects Outstanding
>
The ASPX Code Behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Web;
using System.Web.UI;
using System.Data;
using System.Data.SqlClient;
using System.IO;
namespace DataTablesInWebApplication
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, System.EventArgs e)
{
string dtJSONString;
dtJSONString = @"{";
dtJSONString = dtJSONString + @"""sEcho"": " + Request["sEcho"] + ",";
dtJSONString = dtJSONString + @"""iTotalRecords"": ""2" + @""",";
dtJSONString = dtJSONString + @"""iTotalDisplayRecords"": ""2" + @""",";
dtJSONString = dtJSONString + @"""aaData"":[";
dtJSONString = dtJSONString + "[" + @"""Vendor1""," + @"""10""," +
@"""20""," + @"""30""," + @"""50""],";
dtJSONString = dtJSONString + "[" + @"""Vendor1""," + @"""10""," +
@"""20""," + @"""30""," + @"""50""]";
dtJSONString = dtJSONString + "]}";
HttpContext.Current.Response.Write(dtJSONString);
}
This discussion has been closed.