Derek
Derek

Reputation: 12378

Working with POSTed json objects in Django

I am trying to figure out how to deal with POSTed json objects in Django. I'm POSTing a json object to the server and want to use it like a python dictionary.

here is my js:

$.post(
        "/projects/vote/", 
        obj,
        function(data) {
            //alert("Data Loaded: " + data);
            alert(data["msg"]);
});

What I am returning (end of django view):

return HttpResponse(json.dumps(foo), mimetype="application/json")

where

foo = {"msg": str(postdata)}

In other words, I'm POSTing a json object to the server, and alerting the string of the python object I get on the server so I can see what's going on.

If my obj is:

var obj = {
    'bulk': false,
    'data': {
          'chosen': '14',
          'proj1': '15',
          'proj2': '14',
          'proj3': '16',
          'more': false,


        },
    'data2': [
           {
               'a': 'apple'
           },
           {
               'b': 'banana'
           },
        ],      
  }

I get this in return:

<QueryDict: {u'data[proj3]': [u'16'], u'data[proj2]': [u'14'], u'data[chosen]': [u'14'], u'data[proj1]': [u'15'], u'bulk': [u'false'], u'data2[0][a]': [u'apple'], u'data[more]': [u'false'], u'data2[1][b]': [u'banana']}>

How come the structure of the json obj and python dict don't align? And how do I get the structure to be the same? e.g. instead of data2[0][a], I would get data2 as the key to another dictionary

How I'm getting postdata:

# django version 1.4
postdata = request.POST.copy()

Upvotes: 0

Views: 3417

Answers (4)

Yaguang
Yaguang

Reputation: 702

The server can simply return a string, and the js can be written like this:

$.post(
    "/projects/vote/", 
    obj,
    function(data) {
        data=eval('(' + data+ ')');//get the json object from string.
        //alert("Data Loaded: " + data);
        alert(data["msg"]);
});

Hope this helpful.

Upvotes: 0

rudyryk
rudyryk

Reputation: 3805

You may post json as plain string using JSON.stringify like this:

    $.post(
            "/projects/vote/", 
            {msg: JSON.stringify(obj)},
            function(data) {
                //alert("Data Loaded: " + data);
                alert(data);
    });

Thus on server side you should just extract 'msg' from request.POST:

    def view(request):
          return HttpResponse(request.POST['msg'], mimetype="application/json")

Note, that JSON.stringify is not supported by default in some browsers and you may want to use json lib: https://github.com/douglascrockford/JSON-js

Upvotes: 2

Daniel Roseman
Daniel Roseman

Reputation: 599628

You don't show how you're getting postdata from the POST, but you should be using request.body (request.raw_post_data in versions before 1.4).

Upvotes: 1

Martijn Pieters
Martijn Pieters

Reputation: 1122412

You did not post JSON data. Set the dataType parameter to json:

$.post(
        "/projects/vote/", 
        obj,
        function(data) {
            //alert("Data Loaded: " + data);
            alert(data["msg"]);
        },
        'json'
);

Upvotes: 0

Related Questions