bash-
bash-

Reputation: 6304

jQuery AJAX, json data becomes key in Django, unable to parse the data

I have the following code, using Crockford's json2 to parse the object into json data.

using chrome dev tool the parsed string is "{"query":"asd"}".

However on the django server side, I keep getting an exception when I try to decode the post json data. Turns out the parsed json string became a key in the dictionary.

The query dict from the POST became this: {u'{"query":"asd"}': [u'']}, the json data became the key and the value became an empty string.

Is there a way to rectify this? so the result would be normal json data when the server receives it.

    // convert object to JSON data
    var jsonQuery = JSON.stringify(prod_query); 

    $.ajax({
        type: 'POST',
        url: '/company/product/item_search.json/',
        data: jsonQuery,
        success: //do stuff
                   }
        });
    }
});

python view

query = simplejson.loads(request.POST)

Upvotes: 1

Views: 1460

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599610

You want to pass request.raw_post_data to simplejson.loads.

Upvotes: 3

Related Questions