1

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

我在应用引擎服务器的 python 中得到了一个简单的后处理程序。我正在做一个 jQuery 帖子。两者的代码都是这样的

主文件

import json
...

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

和 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);});

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

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

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

我在这里错过了什么吗?

4

1 回答 1

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);
    }
});
于 2012-10-24T07:31:00.040 回答