How to fix circular reference error from Chrome when using JSON.stringify(oTable.fnGetData())??
How to fix circular reference error from Chrome when using JSON.stringify(oTable.fnGetData())??
ndick2007
Posts: 5Questions: 2Answers: 0
I use JSON.stringify() on the returned data from oTable.fnGetData and it works in Firefox and IE but in Chrome I am getting the following exception. 'columns' also throws the same error in Chrome.
Uncaught TypeError: Converting circular structure to JSON
Code:
var oTable = $("#" + tableId).dataTable();
var columns = oTable.dataTableSettings[0].aoColumns;
var tableData = oTable.fnGetData();
var jsonColumns = JSON.stringify(columns);
var jsonTableData = JSON.stringify(tableData);
This question has accepted answers - jump to:
This discussion has been closed.
Answers
I think this is really a bug in DataTables' legacy API ( fnGetData is legacy -
data()
is the preferred method). It is returning an API instance rather than a plain array, so the code is trying to JSON encode an API instance...What you could do instead is use:
Out of interest, why are you accessing the internal
aoColumns
object? This is internal to DataTables and it could change between versions.Allan
I have created an excel export using Apache POI (needed another way that didn't use flash). I needed it to be generic and independent of the objects that are in the DataTable. To do this, I cycle thru the columns data to get 'sTitle' and 'mData' then I cycle thru the tableData (what I need to change to table.data()) using the 'mData' values as keys to retrieve the data for the respective 'sTitle'. I do this using JSONArray and JSONObject classes in Java code on the server side.
The circular exception is also thrown for aoColumns. Is there a better way to get this data?
Got it - thanks!
I would suggest looping over it with
$.map
or similar and getting only the information you need from it. Otherwise you are going to have a whole load of stuff that you don't need!Allan
I can do that. I prefer Java to javascript, which is why I didn't mind the extra information. Thanks for the help. It would be interesting to know why Firefox and IE ignore this exception that Chrome has an issue with, my code works fine with those two browsers.
No idea to be honest. Perhaps their Javascript engine's just snip the circular reference and get on with it...
Allan