javascript - JSON response not a plain object -
this coming request objects of django apps, not getting plain object, print says string
javascript :
$.getjson("/cadastro/getallpessoas/", function(data){ console.log(data); console.log(typeof(data)); console.log($.isplainobject(data)); //raises error on isarraylike(): $.each(data,function(){ arrayvalues.push([this["pk"],this["fields"]["nome"]]); }) });
console output :
[{"model": "cadastroapp.djangotestpessoa", "pk": 1, "fields": {"nome": "gabriel"}}] string false
views.py :
from django.core import serializers def getallpessoas(request): data = serializers.serialize('json', pessoa.objects.all(), fields=('objectid','nome')) return jsonresponse(data, safe=false)
you're serializing twice in django view, because both serializers.serialize
, jsonresponse convert json. don't that; return normal response serialized value.
return httpresponse(data, content_type='application/json')
Comments
Post a Comment