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

hflaisherhflaisher 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

Answers

  • allanallan Posts: 63,353Questions: 1Answers: 10,444 Site admin

    DataTables' Ajax request sets the dataType option of $.ajax to be json, so jQuery expects and will parse JSON in the response. If you want XML try adding dataType: 'xml' to the ajax object.

    Allan

  • hflaisherhflaisher Posts: 5Questions: 1Answers: 0

    Thanks Allan:

    I had tried that before, no change to the error. Here is what I had tried:

            "ajax" : {
                "dataType": "xml",
                "url": "http://localhost:8080/overview/map/xmldata.txt",
                "dataSrc" : function(xmlData) {
                    return JSON.stringify(xmlToJson(xmlData));
                }
            },
    
  • allanallan Posts: 63,353Questions: 1Answers: 10,444 Site admin

    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

  • hflaisherhflaisher Posts: 5Questions: 1Answers: 0

    Thanks Allan:

    Please see http://maps.cancer.gov/overview/temp12092015/listUpload.html

    The relevant code is around line 268.

    Thank you
    Harvey

  • hflaisherhflaisher Posts: 5Questions: 1Answers: 0

    ... the workaround I am currently attempting is to just use $.ajax({ outside of DataTables, fill a variable and then pass the variable into DT.

  • allanallan Posts: 63,353Questions: 1Answers: 10,444 Site admin
    Answer ✓

    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:

    Invalid XML: <ListBucketResult ...

    Allan

  • hflaisherhflaisher Posts: 5Questions: 1Answers: 0

    Thanks Allan, much appreciated.

This discussion has been closed.