在对jthon应用程序引擎执行jQuery.post时,JSON对象未解码

adi*_*ire 1 jquery google-app-engine json jquery-post python-2.7

我已经在应用引擎上工作了一段时间,从那以后这个问题一直困扰着我.无法找到解决方案,所以我想问过.

我在app引擎服务器的python中有一个简单的post处理程序.我正在做一个jQuery帖子.两者的代码都是这样的

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))
Run Code Online (Sandbox Code Playgroud)

和jQuery帖子

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);});
Run Code Online (Sandbox Code Playgroud)

当我这样做时,我得到以下错误

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
Run Code Online (Sandbox Code Playgroud)

一种解决方法是单独获取请求参数(request.body.get("some_param")),但我发现它很乏味,在处理列表参数时不起作用.

我错过了什么吗?

tpe*_*zek 7

诀窍是你不是发布application/json数据而是application/x-www-form-urlencoded数据,你应该使用这样的代码:

$.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);
    }
});
Run Code Online (Sandbox Code Playgroud)