Iam使用jquery AJAX请求使用post methid将参数发送到龙卷风服务器并做了类似这样的事情:
$.ajax({
type: "POST",
url: "/leave_action?id=" + rowid.id + "&action=" + param,
dataType: "json",
async: false
})
但是当我使用python进行比较时,它无法正常工作:
action = str(self.get_query_argument("action"))
print action #here am getting either approve or decline
if action == "approve":
print "approved"
elif action == "decline":
print "declined"
我也试过is
,但没有运气。
我也尝试过:
action = self.get_query_argument("action")
a=action.encode('ascii','ignore')
但没有用。
答案 0 :(得分:3)
有一点你最好知道! Tornado是一个基于MVC的框架,可以让你掌握很多东西。
因此,您可以使用查询字符串来设置URL模式。这意味着您不需要通过self.get_query_argument()
或self.get_argument()
获得价值。
对于网址:
application = tornado.web.Application([
(r'^(?i)/leave_action/([\w+^/])/([\w+^/])[/]?$', YourLeaveActionHandler)
], **settings)
甚至:
application = tornado.web.Application([
(r'/leave_action/(id)/(approve|decline)', YourLeaveActionHandler, None, "this_url_name")
], **settings)
第二个URL模式稍微好一些。因为我们明确强调第一组应该是id
而第二组应该是approve
或decline
。
在您的处理程序中:
class YourLeaveActionHandler(tornado.web.RequestHandler):
def get(self, id, action):
print id
print action
def post(self, id, action):
print id
print action
if action == "approve":
print "approved"
elif action == "decline":
print "declined"
在您的处理程序中,action
应为approve
或decline
,并且此处理程序中的任何其他内容都不会被接受。因此,您可以轻松比较行动值。