[Fixed]-Sending json data from django-view to ajax

1👍

import the json module

import json

Then Try this in your request method

data = json.dumps([{"square": 0, "key": 0}, {"square": 1, "key": 1}, {"square": 4, "key": 2}])    

return HttpResponse(data, content_type="application/json")

NOTE

Using single quotes as in your snippet would result in the following output

'["{\\"square\\": 0, \\"key\\": 0}", "{\\"square\\": 1, \\"key\\": 1}", "{\\"square\\": 4, \\"key\\": 2}"]'

Its confusing for a seeker , hence i used the above method. But it depends on choice.

0👍

def get_ecommdata(request):
    print "inside get_ecommdata"
    list_square = [
        {"square": 0, "key": 0},
        {"square": 1, "key": 1},
        {"square": 4, "key": 2}
    ]
    data = json.dumps(list_square)
    return HttpResponse(data, content_type='application/json')

Leave a comment