i have page displays data. data displays needs in js array format (representing python nested list).
in order this, use js function fetches data 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, data comes typeof string, , thought use json.parse() pull js array, not work:
var data = getdatafromfiles1(url); console.log(data + " : " + typeof data) data = json.parse(data) console.log(data + " : " + typeof data) and 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 missing obvious? how create js array object data (with flexibility of not dropping data in template tags on load?
you don't show model, guess a.data json - you're double-encoding it.
drop json.dumps , return httpresponse(a.data, content_type="application/javascript").
Comments
Post a Comment