jquery - Retrieve nested javascript objects in Django view as a dict -
i have simple view receives ajax request containing javascript object. jquery request looks this:
$.get(url, {'kwargs': {test: 1}}, function(data){//whatever})
the problem is, request.get contains rather strange key , looks this:
{'kwargs[test]': [1]}
how can decode this? side note, impossible know key (test) beforehand
the expected format obtained python dict looks 1 in request.
i've tried:
request.get.get('kwargs', none)
and i'd expect result:
{'test': 1}
however, none, real key 'kwargs[test]'
edit
i know use kind of regex accomplish this, feels 'reinventing wheel', use case not rare
i recommend using json when communicating , forth between server , client type of situation. json meant handle these types of nested structures in uniform manner.
take @ using jquery $.getjson functionality, http://api.jquery.com/jquery.getjson/
the following example of how structure look...
javscript
var request_data = {kwargs: {test: 1}}; $.getjson(url, {data: json.stringify(request_data)}, function(data){//whatever})
django
import json def your_view(request): my_json = json.loads(request.get['data'])
doing allow parse request contains json data variable of choice (my_json). once assign variable results of json.loads(), have python object containing parsed requested json data , able manipulate object accordingly.
>>> my_json['kwargs'] {u'test': 1}
Comments
Post a Comment