Using sAjaxDataProp with null response results in "aData is undefined"

Using sAjaxDataProp with null response results in "aData is undefined"

platetoneplatetone Posts: 3Questions: 0Answers: 0
edited January 2013 in General
Howdy:

I have sAjaxSource and sAjaxDataProp set like:

[code]
"sAjaxSource": "/rest/roles.json",
"sAjaxDataProp": "roles.assignments"
[/code]

...so that my Datatable implementation uses the 'assignments' collection for its row data.

This works fine if there are values in the assignments collection. However, the REST server I'm connecting to sends back assignments as null if there are no rows available, e.g.:

[code]
{
"roles" : null
}
[/code]

And I get the following error from Datatables:

[code]
TypeError: aData is undefined
[/code]

Obviously it's not happy having a null property while trying to drill down to the data. Is there any way to null check the response before handing off to Datatables?

I was actually able to get past this same problem on another project by creating a Backbone collection for the data source and then using the Backbone parse method to null check and provide an empty collection to Datatables... but I'd like to know if there's a way around it without going that far.

I love Datatables. Thanks!

NBB (Dallas, Texas)

Replies

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    > Is there any way to null check the response before handing off to Datatables?

    Use fnServerData to override the default Ajax call and put your own checking in.

    DataTables does expect the data to be present in sAjaxDataProp's target - for no data it should be an empty array.

    Allan
  • platetoneplatetone Posts: 3Questions: 0Answers: 0
    Thanks for your help! I'll go read up about fnServerData...
  • rlatiganorlatigano Posts: 7Questions: 0Answers: 0
    here is my json return, Im still get error TypeError: aData is undefined, Im using sAjaxSource with fnServerData.
    {"sEcho": 1,"iTotalRecords": 18976,"iTotalDisplayRecords": 18976,"aaData": [["1","1 DESERT RADIOLOGIST- PALOM","HCH 70 B0X 535A ","AMARGOSA VALLEY","Nevada""89020""""Diagnostics (Dx)"],["2","1 DESERT RADIOLOGIST- PALOM","PO BOX 3057 ","INDIANAPOLIS","Indiana""46206""""Diagnostics (Dx)"],["3","1 DESERT RADIOLOGIST- PALOM","PO BOX 36900 ","LAS VEGAS","Nevada""89133""""Diagnostics (Dx)"],["4","1 DESERT RADIOLOGIST- PALOM","PO BOX 52853 ","PHOENIX","Arizona""85072""""Diagnostics (Dx)"],["5","1 DESERT RADIOLOGIST- PALOM","PO BOX 952717 ","SAINT LOUIS","Missouri""63195""""Diagnostics (Dx)"],["6","1 DESERT RADIOLOGIST- PALOM","1299 BERTHA HOWE AVE ","MESQUITE","Nevada""89027""""Diagnostics (Dx)"],["7","1 DESERT RADIOLOGIST- PALOM","1800 W. CHARLESTON BLVD. ","LAS VEGAS","Nevada""89102""""Diagnostics (Dx)"],["8","1 DESERT RADIOLOGIST- PALOM","2020 PALOMINO LANE,STE100 ","LAS VEGAS","Nevada""89110""""Diagnostics (Dx)"],["9","1 DESERT RADIOLOGIST- PALOM","3090 S DURANGO 200 ","LAS VEGAS","Nevada""89117""""Diagnostics (Dx)"],["10","1 DESERT RADIOLOGIST- PALOM","DEPARTMENT 6958 ","LAS ANGELES","Nevada""90084""""Diagnostics (Dx)"]]}"

    please let me know, were is wrong. thnks
  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    Your JSON isn't valid which isn't going to help. Run it through http://jsonlint.com .

    Allan
This discussion has been closed.