Afnan Nazir Afnan Nazir - 1 year ago 79
AngularJS Question

Angular posts whole data to Django view as key in QueryDict

I am using Angular 1.4.4 and Django 1.8. When i post data from Angular to Django view it comes in the form of whole data as key in the QueryDict.

So in view in debug mode when i evaluate request.POST it returns this

<QueryDict: {u'{"messagetitle":"","value":"","valueam":"","wrong_response":"","stop":"","messagekind":"1","undefined":"","messagetype":"3","state":"","language_id":1}': [u'']}>

Notice posted data is inside u'' as key and its value is [u'']

Angular dataservice code is this

function createDraft(data) {
return $http({
url: view_url + 'create_draft/',
method: 'POST',
data: data,
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'

Controller code

app.controller('message',function($scope, $ngBootbox, dataservice, $http){
$scope.formData = {
"messagetitle":"", "value":"", "valueam":"",
"wrong_response": "", "stop": "", "messagekind": "1",
"undefined_response": "", "messagetype":"3",
"state": "", "language_id":1
saveDraft = function(){
function(res) {
$ngBootbox.alert("<div class='alert-success alert_box'>Draft Saved</div>")
function(res) {
var error_message = res['statusText'].trim();
if (error_message === '')
error_message = 'Error Occurred';
$ngBootbox.alert("<div class='alert-warning alert_box'>" + error_message + "</div>")

saveDraft function is called from angular template(which is html form) on button click.

Let me know what i am doing wrong here. I have tried some answers on stackoverflow but could not find any question/answer which addresses my specific problem.

I know i can do json.loads(request.body) but i need to post data in django's request.POST

Answer Source

Use $.param(data) instead of data in your service

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download