JavaScript
客户端发送如下请求:
$.ajax({
url: 'http://localhost:7973/test',
type: 'GET',
data: {'host': 'mike', 'guests': {'name': ['car', 'ball'], 'age': [6, 10, 7]}},
success: function(result){alert(result)},
error: function(error){alert(error)}
});
Python
服务器使用tornado
处理请求:
import tornado.ioloop
import tornado.web
class TestHandler(tornado.web.RequestHandler):
def get(self):
host = self.get_argument('host')
print(host)
guests = self.get_argument('guests')
print(guests)
def make_app():
return tornado.web.Application([
(r'/test', TestHandler)
])
if __name__ == "__main__":
app = make_app()
port = 7973
app.listen(port)
print('-' * 100)
print('server started, listening to ', port, '...\n')
tornado.ioloop.IOLoop.current().start()
服务器端的输出如下。显然,“主机”参数已成功获得,但我不知道如何获取其值是复杂对象本身的参数(例如数组或字典)。请向我解释这些数据结构之间的强制转换和转储及其字符串表示的机制吗?我读了tornado document,但找不到答案。
迈克
警告:tornado.general:400 GET / test?host = mike&guests%5Bname%5D%5B%5D = car&guests%5Bname%5D%5B%5D = ball&guests%5Bage%5D%5B%5D = 6&guests%5Bage%5D%5B%5B%5D = 10&guests%5Bage%5D %5B%5D = 7 (:: 1):缺少参议嘉宾
警告:tornado.access:400 GET / test?host = mike&guests%5Bname%5D%5B%5D = car&guests%5Bname%5D%5B%5D = ball&guests%5Bage%5D%5B%5D = 6&guests%5Bage%5D%5B%5B%5D = 10&guests%5Bage%5D %5B%5D = 7 (:: 1)1.99ms
答案 0 :(得分:2)
您可以将json 对象转换为json string 。
更改
data: {'host': 'mike', 'guests': {'name': ['car', 'ball'], 'age': [6, 10, 7]}},
到
data: JASON.stringify({'host': 'mike',
'guests': {'name': ['car', 'ball'],
'age': [6, 10, 7]}}),
然后在服务器端可以执行以下操作:
guests_string = self.get_argument('guests')
guests = json.loads(guests_string)
guests
应该是您可以在Python中执行任何操作的字典。