Hi I am parsing serialzed json -
def wall_copy(request):
posts = user_post.objects.order_by('id')[:20].reverse()
posts_serialized = serializers.serialize('json', posts)
return JsonResponse(posts_serialized, safe=False)
and trying to get data as-
$(document).ready(function()
{
setInterval(function()
{
var xhr = new XMLHttpRequest();
xhr.open("GET","http://10.8.21.17:8000/wall/wall_copy/",false);
xhr.send(null);
var data = JSON.parse(xhr.responseText);
//alert(xhr.status+' '+xhr.statusText);
for(i=0; i<20; i++)
{
alert(data[i].post_content);
}
}, 3000);
});
But the problem is that every time it alerts as 'Undefined'. I checked the url and the server is sending json data but I am unable to fetch it.
Json data-
"[{\"fields\": {\"post_id\": \"rahularyan06:16PM on February 26, 2015\", \"posted_by\": \"rahularyan\", \"post_content\": \"koi nhi re\", \"time_of_post\": \"2015-02-26T18:16:00Z\"}, \"model\": \"wall.user_post\", \"pk\": 77}, {\"fields\": {\"post_id\": \"aquaman06:00PM on February 26, 2015\", \"posted_by\": \"aquaman\", \"post_content\": \"kuch nhi\", \"time_of_post\": \"2015-02-26T18:00:44Z\"}, \"model\": \"wall.user_post\", \"pk\": 76}, {\"fields\": {\"post_id\": \"rahularyan06:00PM on February 26, 2015\", \"posted_by\": \"rahularyan\", \"post_content\": \"kya hua??\", \"time_of_post\": \"2015-02-26T18:00:04Z\"}, \"model\": \"wall.user_post\", \"pk\": 75}, {\"fields\": {\"post_id\": \"aquaman12:01AM on February 26, 2015\", \"posted_by\": \"aquaman\", \"post_content\": \"lelo\", \"time_of_post\": \"2015-02-26T00:01:14Z\"}, \"model\": \"wall.user_post\", \"pk\": 74}, {\"fields\": {\"post_id\": \"aquaman10:41AM on February 25, 2015\", \"posted_by\": \"aquaman\", \"post_content\": \"sahi me lega??\", \"time_of_post\": \"2015-02-25T10:41:43Z\"}, \"model\": \"wall.user_post\", \"pk\": 73}, {\"fields\": {\"post_id\": \"aquaman10:41AM on February 25, 2015\", \"posted_by\": \"aquaman\", \"post_content\": \"sahi me lega??\", \"time_of_post\": \"2015-02-25T10:41:34Z\"}, \"model\": \"wall.user_post\", \"pk\": 72}, {\"fields\": {\"post_id\": \"shreyansh10:41AM on February 25, 2015\", \"posted_by\": \"shreyansh\", \"post_content\": \"yo\", \"time_of_post\": \"2015-02-25T10:41:17Z\"}, \"model\": \"wall.user_post\", \"pk\": 71}, {\"fields\": {\"post_id\": \"aquaman10:40AM on February 25, 2015\", \"posted_by\": \"aquaman\", \"post_content\": \"muh me lega??\", \"time_of_post\": \"2015-02-25T10:40:53Z\"}, \"model\": \"wall.user_post\", \"pk\": 70}, {\"fields\": {\"post_id\": \"aquaman10:40AM on February 25, 2015\", \"posted_by\": \"aquaman\", \"post_content\": \"bc\", \"time_of_post\": \"2015-02-25T10:40:42Z\"}, \"model\": \"wall.user_post\", \"pk\": 69}, {\"fields\": {\"post_id\": \"shreyansh10:40AM on February 25, 2015\", \"posted_by\": \"shreyansh\", \"post_content\": \"lele\", \"time_of_post\": \"2015-02-25T10:40:28Z\"}, \"model\": \"wall.user_post\", \"pk\": 68}, {\"fields\": {\"post_id\": \"aquaman10:39AM on February 25, 2015\", \"posted_by\": \"aquaman\", \"post_content\": \"kya bhai??\", \"time_of_post\": \"2015-02-25T10:39:39Z\"}, \"model\": \"wall.user_post\", \"pk\": 67}, {\"fields\": {\"post_id\": \"aquaman10:38AM on February 25, 2015\", \"posted_by\": \"aquaman\", \"post_content\": \"sb mast\", \"time_of_post\": \"2015-02-25T10:38:22Z\"}, \"model\": \"wall.user_post\", \"pk\": 66}, {\"fields\": {\"post_id\": \"shreyansh10:38AM on February 25, 2015\", \"posted_by\": \"shreyansh\", \"post_content\": \"or baaki??\", \"time_of_post\": \"2015-02-25T10:38:05Z\"}, \"model\": \"wall.user_post\", \"pk\": 65}, {\"fields\": {\"post_id\": \"aquaman10:03AM on February 25, 2015\", \"posted_by\": \"aquaman\", \"post_content\": \"badiya\", \"time_of_post\": \"2015-02-25T10:03:39Z\"}, \"model\": \"wall.user_post\", \"pk\": 64}, {\"fields\": {\"post_id\": \"shreyansh10:03AM on February 25, 2015\", \"posted_by\": \"shreyansh\", \"post_content\": \"thik ba\", \"time_of_post\": \"2015-02-25T10:03:23Z\"}, \"model\": \"wall.user_post\", \"pk\": 63}, {\"fields\": {\"post_id\": \"aquaman10:00AM on February 25, 2015\", \"posted_by\": \"aquaman\", \"post_content\": \"kaisan??\", \"time_of_post\": \"2015-02-25T10:00:30Z\"}, \"model\": \"wall.user_post\", \"pk\": 62}, {\"fields\": {\"post_id\": \"aquaman10:00AM on February 25, 2015\", \"posted_by\": \"aquaman\", \"post_content\": \"hi\", \"time_of_post\": \"2015-02-25T10:00:04Z\"}, \"model\": \"wall.user_post\", \"pk\": 61}, {\"fields\": {\"post_id\": \"aquaman09:58AM on February 25, 2015\", \"posted_by\": \"aquaman\", \"post_content\": \"abe jldi likh\", \"time_of_post\": \"2015-02-25T09:58:43Z\"}, \"model\": \"wall.user_post\", \"pk\": 60}, {\"fields\": {\"post_id\": \"aquaman09:57AM on February 25, 2015\", \"posted_by\": \"aquaman\", \"post_content\": \"hello\", \"time_of_post\": \"2015-02-25T09:57:49Z\"}, \"model\": \"wall.user_post\", \"pk\": 59}, {\"fields\": {\"post_id\": \"shreyansh09:39AM on February 25, 2015\", \"posted_by\": \"shreyansh\", \"post_content\": \"lele\", \"time_of_post\": \"2015-02-25T09:39:39Z\"}, \"model\": \"wall.user_post\", \"pk\": 58}]"
Help me how can I get this data through javascript.
Thanks in advance.
datastructure to the console?console.log(data)?