[Django]-Parsing json object sent with Ajax GET method in Django

0๐Ÿ‘

โœ…

Ok, I solved it like this:

changed my Ajax request from GET to POST,
followed this to acquire CSRF_token,
used this parser to parse my object,
and finally changed my Python function:

from django.views.decorators.csrf import csrf_exempt

@csrf_exempt
def startNewVoting(request):
  from querystring_parser import parser
  p = parser.parse(request.POST.urlencode())
  for key, track in p['tracks'].iteritems():
    print track
    # save to db...
๐Ÿ‘คerrata

5๐Ÿ‘

You can solve it by using json encoding:

encode in javascript

data: {tracks: JSON.stringify(tracks)}

decode in the view

tracks = json.loads(request.POST.get('tracks'))

This way you avoid 3rd party parser ๐Ÿ™‚

๐Ÿ‘คmariodev

Leave a comment