DataTables warning
DataTables warning
Thanks for the help.
So I'm not even sure I can do it this way. It appears that older version were able to do this. Using .Net 4.0 and my code behind file is returning a JavaScriptSerializer string. I did go to the URL, but I wasn't understanding. Below is the warning
DataTables warning: table id=studentsTab - Requested unknown parameter 'StudentID' for row 0, column 0. For more information about this error, please see http://datatables.net/tn/4
<table id="studentsTab" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th>Student ID</th>
<th>Student Name</th>
<th>Absent</th>
<th>Gender</th>
<th>Grade</th>
<th>Accommodation</th>
<th>Test Date</th>
<th>Session #</th>
<th>Room</th>
<th>Barcode</th>
<th>Booklet</th>
</tr>
</thead>
</table>`
$.ajax({
type: "POST",
url: 'TestView.aspx/GetStudentsTabData',
data: JSON.stringify({ TestID: testID }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
$('#studentsTab').DataTable({
data: data.d,
columns: [
{ data: "StudentID" },
{ data: "StudentName" },
{ data: "Absent" },
{ data: "Gender" },
{ data: "Grade" },
{ data: "Accommodation" },
{ data: "TestDate" },
{ data: "SessionNumber" },
{ data: "Room" },
{ data: "Barcode" },
{ data: "Booklet" }
],
});
},
error: function (e) {
console.log("Something Wrong. In GetStudentsTabData function\n" + e.d);
},
complete: function () {
}
})
I'm return some dummy data:
{"d":"[{\"StudentID\":\"654321\",\"StudentName\":\"Tim Testing\",\"Absent\":\"Yes\",\"Gender\":\"Male\",\"Grade\":12,\"Accommodation\":\"Yes\",\"TestDate\":\"10/30/2018\",\"SessionNumber\":1,\"Room\":\"101\",\"Barcode\":\"987654321\",\"Booklet\":\"1346579\"},{\"StudentID\":\"654321\",\"StudentName\":\"Tim Testing\",\"Absent\":\"Yes\",\"Gender\":\"Male\",\"Grade\":12,\"Accommodation\":\"Yes\",\"TestDate\":\"10/30/2018\",\"SessionNumber\":1,\"Room\":\"101\",\"Barcode\":\"987654321\",\"Booklet\":\"1346579\"}]"}
This question has accepted answers - jump to:
Answers
Hi @54696d20 ,
Yep, that data is the problem - the .NET mucks it up so you'll need to strip out any offending characters. Rion Williams's reply on this thread should help.
Cheers,
Colin
Thanks @colin . Tried that, with no luck. Don't you need the quotes?
No, that's an oddity of the .NET stuff - there was another thread on that earlier this year but I couldn't it. Could you paste the data now that it's modified, please.
C
Here you go:
{"d":"[{StudentID:123456,StudentName:Tony Tiger,Absent:Yes,Gender:Male,Grade:12,Accommodation:Yes,TestDate:10/30/2018,SessionNumber:1,Room:101,Barcode:123456789,Booklet:98765431}]"}
@colin Yes, .net does add a back slash. since the string is wrapped in quotes. It's their form of escaping rules
I thought that this would work
var formattedString = JSON.stringify(data).replace(/\/g, '');
$('#studentsTab').DataTable({
data: formattedString,
columns: [
{ data: "StudentID" },
{ data: "StudentName" },
{ data: "Absent" },
{ data: "Gender" },
{ data: "Grade" },
{ data: "Accommodation" },
{ data: "TestDate" },
{ data: "SessionNumber" },
{ data: "Room" },
{ data: "Barcode" },
{ data: "Booklet" }
],
});
formattedString =
{"d":"[{"StudentID":"123456","StudentName":"Tony Tiger","Absent":"Yes","Gender":"Male","Grade":12,"Accommodation":"Yes","TestDate":"10/30/2018","SessionNumber":1,"Room":"101","Barcode":"123456789","Booklet":"98765431"}]"}
Yep, that's still the wrong format, if you follow Rion's example it should be something like:
As it stands, the JSON is just a string
C
Following Rion's example:
var removeSlashes = serializer.Serialize(listTabData).Replace("\"", "");
This gives me:
{"d":"[{StudentID:123456,StudentName:Tony Tiger,Absent:Yes,Gender:Male,Grade:12,Accommodation:Yes,TestDate:10/30/2018,SessionNumber:1,Room:101,Barcode:123456789,Booklet:98765431}]"}
In your example. You have quotes
Rion's code is a bit different than yours:
In your code you are replacing
\"
with nothing. But you should only be replacing\
. Try changing.Replace("\"", "");
to.Replace("\", "");
removing the"
after the backslash.Kevin
@kthorngren He's got two answers. The one you provided gives me:
{"d":"[{\"StudentID\":\"123456\",\"StudentName\":\"Tony Tiger\",\"Absent\":\"Yes\",\"Gender\":\"Male\",\"Grade\":12,\"Accommodation\":\"Yes\",\"TestDate\":\"10/30/2018\",\"SessionNumber\":1,\"Room\":\"101\",\"Barcode\":\"123456789\",\"Booklet\":\"98765431\"}]"}
Which doesn't work and is the same as if I didn't add it.
So I've been playing around. I'm having it return
{"d":"[{\"StudentID\":\"123456\",\"StudentName\":\"Tony Tiger\",\"Absent\":\"Yes\",\"Gender\":\"Male\",\"Grade\":12,\"Accommodation\":\"Yes\",\"TestDate\":\"10/30/2018\",\"SessionNumber\":1,\"Room\":\"101\",\"Barcode\":\"123456789\",\"Booklet\":\"98765431\"}]"}
With some changes to the string
var format = '{"d":' + JSON.stringify(data).substring(6).replace(/\/g, '');
I get:
{"d":[{"StudentID":"123456","StudentName":"Tony Tiger","Absent":"Yes","Gender":"Male","Grade":12,"Accommodation":"Yes","TestDate":"10/30/2018","SessionNumber":1,"Room":"101","Barcode":"123456789","Booklet":"98765431"}]"}
Which is really close to what @colin provided. STILL doesn't work. I don't get why this is difficult lol
Your suggestion
.Replace("\", "");
isn't going to work, because C# will think you are escaping ". That's why you do something like.Replace(@"\", "");
Which doesn't work eitherCopy your JSON string into https://jsonlint.com/ and you will see that it is still not a valid JSON string. The trailing
"
should not be there:"98765431"}]"}
.Kevin
I understand. I made some for changes Thanks for the site @kthorngren
{"data":[{"StudentID":"123456","StudentName":"Tony Tiger","Absent":"Yes","Gender":"Male","Grade":12,"Accommodation":"Yes","TestDate":"10/30/2018","SessionNumber":1,"Room":"101","Barcode":"123456789","Booklet":"98765431"},{"StudentID":null,"StudentName":null,"Absent":null,"Gender":null,"Grade":0,"Accommodation":null,"TestDate":null,"SessionNumber":0,"Room":null,"Barcode":null,"Booklet":null}]}
Exactally the way @colin has it and it STILL doesn't work.
Error:
DataTables warning: table id=studentsTab - Requested unknown parameter 'StudentID' for row 0, column 0. For more information about this error, please see http://datatables.net/tn/4
Your
data
might be passed to the ajaxsuccess
function as a string. In that cae you will need to use something like this to convert it to Javascript data as the first line in the function.data = JSON.parse(data);
Kevin
Thanks for the information @kthorngren
I don't get an error, but nothing displays either.
One other thing, you need to pass
formattedString.d
into thedata
, as it's the array DataTables wants. See here.Boom!! Adding that format.d worked.. THANKS @colin and @kthorngren
This is awesome!
We get there eventually
Just sucks that you have to jump through a bunch of loops. That's why I did the changes on the front end
var format = '{"d":' + JSON.stringify(data).substring(6).replace(/\/g, '');
format = format.substring(0, format.length - 2) + '}'
format = JSON.parse(format);
After the stored procedure is completed. I'll see how efficient this is.
Thanks again
Good to hear you found a solution. For my own interest, could you do:
just before
var format = ...
. I'd like to see the exact JSON format used. Its really brittle to use text manipulation functions of a JSON string.Allan
@allan Sure,
{"d":"[{\"StudentID\":\"123456\",\"StudentName\":\"Tony Tiger\",\"Absent\":\"Yes\",\"Gender\":\"Male\",\"Grade\":12,\"Accommodation\":\"Yes\",\"TestDate\":\"10/30/2018\",\"SessionNumber\":1,\"Room\":\"101\",\"Barcode\":\"123456789\",\"Booklet\":\"98765431\"}]"}
I've never understood why Microsoft decided to wrap valid JSON up as a string inside more valid JSON... What you could do here is use:
That will just convert the
data.d
string to a real JSON array and pass that in as the array data source to DataTables. A little easier that the workaround you've come up with above.Allan
Nice.. that works. Thanks @allan. .NET does add a back slash if quotes are inside quotes. Since the string is wrapped in quotes. The \ is their form of escaping rules. It would be really nice if they allow single quotes.
Thanks again