Sending post data from angularjs to django as JSON and not as raw content Sending post data from angularjs to django as JSON and not as raw content python python

Sending post data from angularjs to django as JSON and not as raw content


When calling ajax, you recieve encoded json string in request body, so you need to decode it using python's json module to get python dict:

json.loads(request.body)


In my case works something like

$http({    url: '/url/',    method: "POST",    data: $.param(params),    headers: {        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'    }})

Or more nice variant:

app.config ($httpProvider) ->    ...    $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'

and then

$scope.save_result = $http.post('/url/', $.param(params))

http://www.daveoncode.com/2013/10/17/how-to-make-angularjs-and-django-play-nice-together/


I am using zope2 where I used simplejson to decode the request json into python dictionary as:

request_dict = simplejson.loads(request.get('BODY','')

It's working correctly for me. In this way I am able to use angularjs default json request rather than converting it into form post.