I have a page which displays some data. The data it displays needs to be in JS Array format (representing the python nested list).
In order to do this, I use a JS function that fetches the data from a django view:
js function:
var getDataFromFiles1 = function(theUrl) {
$.ajaxSetup({async: false});
var xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", theUrl, false );
xmlHttp.send( null );
return xmlHttp.responseText;
};
views.py:
a = MyModel.objects.get(c=b, x=y)
json_object = json.dumps(a.data)
return HttpResponse(json_object, content_type="application/javascript")
However, the data that comes back is typeof string, and I thought that I could just use JSON.parse() to pull into a JS Array, however this does not work:
var data = getDataFromFiles1(url);
console.log(data + " : " + typeof data)
data = JSON.parse(data)
console.log(data + " : " + typeof data)
and the logging included above gives:
"[[\"example\", \"example\", \"example\", \"example\", \"not set\"], [\"example\", \"example\", \"example\", \"example\", \"not set\"]]" : string
and
[["example", "example", "example", "example", "not set"], ["example", "example", "example", "example", "not set"]] : string
Am I missing something obvious? How do I create a JS Array object with this data (with the flexibility of not dropping the data in with template tags on load?