2012-01-07 43 views
0

我只是想序列化提交的表单的各个领域,像这样在PHP:序列化表单提交值

json_encode($_GET) 

json.dumps(self.request.get)不起作用:

<type 'exceptions.TypeError'>: <bound method Request.get of <Request at 77ea190 GET http://localhost:8083/?a=value>> is not JSON serializable 
     args = ('<bound method Request.get of <Request at 77ea190...ocalhost:8083/?a=value>> is not JSON serializable',) 
     message = '<bound method Request.get of <Request at 77ea190...ocalhost:8083/?a=value>> is not JSON serializable' 

我我尝试过使用CGI模块,这也给了一个不可序列化的错误。我想知道的情况是:我使用它将数据发送回我的模板以重新填充表单域。

+0

对于URLEncode这些参数比对JSON进行编码更有意义 - 在这种情况下,您可以使用'self.request.query'以原始形式获取它们。 – 2012-01-09 05:41:50

+0

@NickJohnson 我会这样做,如果我不使用称为填充的jQuery插件。它照顾我的每一件事;它只是想要一个表单内容的JSON字符串。这非常方便:http://tinyurl.com/lmblvv – ofko 2012-01-09 06:45:30

回答

5

self.request.get返回get方法,而不是返回的方法。你必须这样做:

json.dumps(self.request.GET.items()) 

request.GET会返回一个UnicodeMultiDict对象,request.GET.items()会返回一个元组列表,每个元组是(key, value)。

参考http://code.google.com/appengine/docs/python/tools/webapp/requestclass.htmlhttp://docs.webob.org/en/latest/reference.html#id1

+0

谢谢,我现在可以回去工作了;) – ofko 2012-01-07 02:18:45