Unable to use DataSrc property of ajax object to convert XML to JSON
Unable to use DataSrc property of ajax object to convert XML to JSON
hflaisher
Posts: 5Questions: 1Answers: 0
Hello:
This page: https://datatables.net/examples/ajax/custom_data_flat.html
implies that I can use the dataSrc property of ajax to convert incoming data to JSON before feeding it to the DataTable. However, I get the "Invalid JSON Response" error, and breakpoints show it is not getting into the dataSrc conversion function below. Can someone please help?
thank you in advance
$('#abc').DataTable({
"ajax" : {
"url": "http://localhost:8080/overview/map/xmldata.txt",
"dataSrc" : function(xmlData) {
return JSON.stringify(xmlToJson(xmlData));
}
},
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
DataTables' Ajax request sets the
dataType
option of$.ajax
to bejson
, so jQuery expects and will parse JSON in the response. If you want XML try addingdataType: 'xml'
to theajax
object.Allan
Thanks Allan:
I had tried that before, no change to the error. Here is what I had tried:
Could you give me a link to the page showing the issue so I can debug it please? That looks like it should work as expected.
Allan
Thanks Allan:
Please see http://maps.cancer.gov/overview/temp12092015/listUpload.html
The relevant code is around line 268.
Thank you
Harvey
... the workaround I am currently attempting is to just use $.ajax({ outside of DataTables, fill a variable and then pass the variable into DT.
Hi,
It looks like the issue is that jQuery is unable to parse the XML file. It then reports a
parseerror
, and DataTables shows an error message stating that JSON couldn't be parsed. The error message is wrong - it is the XML that couldn't be parsed.I'm not entirely sure why it isn't parsing the XML - but this is the error jQuery is throwing:
Allan
Thanks Allan, much appreciated.