1

I have a javascript object that looks like this:

var myObject = { "Danny": {"height": 1.70, "weight" : 70 }, "David" : {"height": 1.90, "weight" : 80" } ... }

I want to send it as a JSON to a django view and parse it in it. On the client side, using jQuery, I added this:

      $.ajax({
          type: "POST",
          url: "/ajax/someview",
          data: JSON.stringify(myObject),
          contentType: 'application/json; charset=utf-8',
          dataType: "json"
      });

However, when I debug the view and look at request.POST, the data looks like this:

POST:<QueryDict: {u'{"Danny": {"height": 1.70, "weight" : 70 }, "David" : {"height": 1.90, "weight" : 80" }}' : [u'']}>

How can I convert this to a python object which i can work it (using simplejson.load)?

Thanks, Joel

2 Answers 2

4

You can access the raw POST data with - not surprisingly - request.raw_post_data. That will give you a string you can convert with simplejson.loads().

Sign up to request clarification or add additional context in comments.

Comments

1
$.ajax({
      type: "POST",
      url: "/ajax/someview",
      data: {'mydata': JSON.stringify(myObject)},
      contentType: 'application/json; charset=utf-8',
      dataType: "json"
});

Then get it with request.POST.get('mydata')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.