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服务器处理请求:
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,但找不到答案.
mike
WARNING: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%5D=10&guests%5Bage%5D%5B%5D=7
(::1): Missing argument guestsWARNING: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%5D=10&guests%5Bage%5D%5B%5D=7
(::1) 1.99ms
最佳答案
您可以将json对象转换为json字符串.
原文链接:https://www.f2er.com/js/531130.html更改
data: {'host': 'mike',
至
data: JASON.stringify({'host': 'mike',7]}}),
然后在服务器端,您可以执行以下操作:
guests_string = self.get_argument('guests')
guests = json.loads(guests_string)
来宾应该是一本字典,您可以使用Python进行任何处理.