I'm running into a problem with implementing a jQuery bootgrid into my ASP.Net Webforms application.
I get the following problem on my page load which prevents the bootgrid from loading data:
Uncaught SyntaxError: Unexpected token < in JSON at position 4
at Function.parse [as parseJSON] ()
at Object.success (jquery.bootgrid.js:232)
at fire (jquery-3.1.1.js:3305)
at Object.fireWith [as resolveWith] (jquery-3.1.1.js:3435)
at done (jquery-3.1.1.js:9242)
at XMLHttpRequest.
This is my bootgrid implementation in the JavaScript:
$("#grid").bootgrid({
ajax: true,
url: "/Secure/Maintenance/Roles.aspx/GetData",
rowCount: [10, 50, 75, 100, 200, -1]
})
And then here is my C# WebMethod
:
[WebMethod]
public static string GetData()
{
var results = (from x in EFDB.AspNetRoles
select x).AsQueryable();
return JsonConvert.SerializeObject(results);
}
I've used a LINQ Query to get the data and tried converting it to JSON but I'm not sure if I'm doing it correctly. When I set breakpoints in the GetData
method, none of them get reached. So I'm really having trouble debugging this.
Any advice on what I could be doing wrong?
Thanks!
I ended up creating an Generic Handler
to post the data and that worked well. Here's is the full code for the Generic Handler
:
public class RolesHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/json";
context.Response.Write(GetData());
}
public bool IsReusable
{
get
{
return false;
}
}
public string GetData()
{
var result = string.Empty;
var con = new SqlConnection();
var cmd = new SqlCommand();
var dt = new DataTable();
string sSQL = @"SELECT Id, Name
FROM dbo.AspNetRoles;";
try
{
using (var connection = THF.Models.SQLConnectionManager.GetConnection())
{
using (var command = new SqlCommand(sSQL, connection))
{
connection.Open();
command.CommandTimeout = 0;
var da = new SqlDataAdapter(command);
da.Fill(dt);
}
}
var sNumRows = dt.Rows.Count.ToString();
var sDT = JsonConvert.SerializeObject(dt);
result = "{ \"current\": 1, \"rowCount\": 10, \"rows\": " + sDT + ", \"total\": " + sNumRows + " }";
}
catch (Exception ex)
{
}
finally
{
cmd.Dispose();
THF.Models.SQLConnectionManager.CloseConn(con);
}
return result;
}