我正在self.render
渲染一个html模板,它依赖于通过ajax从客户端从客户端接收的信息,def post()
如下所示:
class aHandler(BaseHandler):
@tornado.web.authenticated
def post(self):
taskComp = json.loads(self.request.body)
if taskComp['type'] == 'edit':
if taskComp['taskType'] == 'task':
self.render(
"tasks.html",
user=self.current_user,
timestamp='',
projects='',
type='',
taskCount='',
resName='')
但是,这不会将用户重定向到html页面'tasks.html'。
但是我在控制台中看到一个状态:
[I 141215 16:00:55 web:1811] 200 GET /tasks (127.0.0.1)
其中“ / tasks”是task.html的别名
为什么不将其重定向?
还是如何使用从ajax接收的数据以及上面self.render
请求中提供的所有参数将其重定向到task.html页面?
“渲染”从不将访问者的浏览器重定向到其他URL。它向浏览器显示您呈现的页面的内容,在本例中为“ tasks.html”模板。
重定向浏览器:
@tornado.web.authenticated
def post(self):
self.redirect('/tasks')
return
要使用AJAX响应进行重定向,请尝试将目标位置从Python发送到Javascript:
class aHandler(BaseHandler):
@tornado.web.authenticated
def post(self):
self.write(json.dumps(dict(
location='/tasks',
user=self.current_user,
timestamp='',
projects='',
type='',
taskCount='',
resName='')))
然后在Javascript中的AJAX响应处理程序中:
$.ajax({
url: "url",
}).done(function(data) {
var url = data.location + '?user=' + data.user + '×tamp=' + data.timestamp; // etc.
window.location.replace("http://stackoverflow.com");
});
有关URL编码的更多信息,请参见此答案。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句