I am trying to return a JSON response in Django that is called with Ajax. The JSON response will simply be a list with each index containing some sort of HTML code. I am running into some errors when I try to parse this response in JQuery after I have successfully pulled it with Ajax. The Chrome developer tools simply tell me this error when I try to log the received JSON response in the console Uncaught SyntaxError: Unexpected token &
Here is the code that I use to return a JSON response from Django that contains HTML code inside of it.
template = loader.get_template('home_post.html')
post_arr = []
for post in posts:
context = Context({'image': post.image, 'body': post.body})
post_arr.append(escape(template.render(context)))
return HttpResponse(json.dumps(post_arr), content_type='application/json')
Is there any way that I should be doing this other than what I am doing right now? As in, is there a built in way to escape HTML code so that I can put it in a JSON response?