Gustavo,

I believe this is a problem (feature?) of jquery. It is not
serializing your full settings object to a string. Instead, it creates
a series of url-encoded key value pairs. It looks like it is just
calling .toString() on the value side as well. When .toString() is
called on:

{"id": {"label":"", "width":"10px"}, "name":{"label":"Nome",
"width":"300px;"}}

the response is simply "[object Object]". This is what is actually
being sent to Django. It looks like jquery does this so that all the
data can be encoded into the URL string if this was sent as a GET
request.

If you must send a full JSON object, one possibility is to send your
JSON structure as a string and just decode the full raw_post_data in
Django. This might look like the below code:

def datagrid(request):
    try:
        jsondict = json.loads(request.raw_post_data)
    except Exception, e:
        logging.error(e)
        jsondict = {}
    logging.info(jsondict)
    return HttpResponse(json.dumps(jsondict, indent=2))

Your jquery code could look like this although there's probably a
better way to do it:

obj = '{"model_name": "Client", "cols": {"id": {"label":"",
"width":"10px"}, "name": {"label":"Nome", "width":"300px;"}}}';

$.ajax({
                    type: "POST",
                    url: "/datagrid/",
                    dataType: "text",
                    data: obj,
                    processData: false,
                    success: function(data, textStatus) {
                        alert(data);
                    }
})

If you run into issues like this in the future, log the full
raw_post_data and you can see what is being transmitted from client to
server.

-David


On Jul 16, 1:32 pm, Gustavo Henrique <gustavo...@gmail.com> wrote:
> Hi list!
> I'm a problem with json and django. My javascript code send all data
> by ajax but my view don't accept the content [Object Object] like json
> object.
> I try using jquery:
>
> settings = {
>         model_name: 'Client',
>         cols: {"id": {"label":"", "width":"10px"}, "name":
> {"label":"Nome", "width":"300px;"}}
>
> }
>
> $.ajax({
>             type: 'POST',
>             url: '/datagrid/',
>             dataType: 'json',
>             data: settings,
>             success: function(r) {
>             ....
>
> Django raise exception:
> Exception Value: Expecting object: line 1 column 1 (char 1)
> Exception Location: /usr/lib/python2.5/site-packages/django/utils/
> simplejson/decoder.py in JSONArray, line 221
>
> in my view I use:
>
> def myview(request)
>     P = request.POST
>     cols = "%s" % simplejson.loads(P.get('cols'))
>
> but the request's content is:
> <QueryDict: {u'model_name': [u'Client'], u'cols': [u'[object Object]']}
>
>
>
> Can you help me?
>
> Thanks!
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to