Search code examples
jqueryjsongoogle-app-enginepython-2.7jquery-post

JSON object not decoded while doing jQuery.post to python app engine


I have been working on the app engine for some time, and this problem has been bugging me ever since. Could not find a solution to this, so thought I had ask.

I got a simple post handler in python in the app engine server. I am doing a jQuery post. The code for both goes something like this

main.py

import json
...

class SomeHandler(webapp2.RequestHandler):
   def post(self):
      data = json.loads(self.request.body)
      return self.response.out.write(json.dumps(data))

And the jQuery post

jQuery.post('/quiz',
{name:'some problem 2',desc:'some submitted 2',questions:[{question:'question1'}]},
function(data,textStatus, jqXHR){console.log('POST response: ');console.log(data);});

When I do that, I get the following error

Traceback (most recent call last):
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 1536, in __call__
    rv = self.handle_exception(request, response, e)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 1530, in __call__
    rv = self.router.dispatch(request, response)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 1102, in __call__
    return handler.dispatch()
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 572, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 570, in dispatch
    return method(*args, **kwargs)
  File "/Users/adityarao/appengine/Quiz_1/main.py", line 121, in post
    data = json.loads(self.request.body)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 326, in loads
    return _default_decoder.decode(s)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 366, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 384, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

One workaround is to get the request parameters separately (request.body.get("some_param")), but I find it tedious and doesn't work when dealing with list parameters.

Have I missed something here?


Solution

  • The trick is you are not posting application/json data but application/x-www-form-urlencoded data, you should use code like this:

    $.ajax({
        type: 'POST',
        url: '/quiz',
        data: JSON.stringify({ name: 'some problem 2', desc: 'some submitted 2', questions: [ { question: 'question1' } ] }),
        contentType: 'application/json',
        success: function(data,textStatus, jqXHR) {
            console.log('POST response: ');
            console.log(data);
        }
    });