Reputation:
I have a handler does not work, but it should work with ajax need orientation.
sample:
from tornado.web import RequestHandler
class MyHandler(RequestHandler):
def get(self):
self.write("ok!")
if not MyHandler == Ajax request then Redirect "/" ??
thank you...
Upvotes: 5
Views: 2782
Reputation: 494
check out this: this one should be more easier to understand:
https://github.com/zhanglongqi/TornadoAJAXSample
Upvotes: 1
Reputation: 2533
so; Decorator you can use. create
decorators.py
add is_ajax function;
# decorators.py
def is_ajax(method):
@wraps(method)
def wrapper(self, *args, **kwargs):
if "X-Requested-With" in self.request.headers:
if self.request.headers['X-Requested-With'] == "XMLHttpRequest":
return method(self, *args, **kwargs)
else:
self.redirect("/")
return wrapper
and
from tornado.web import RequestHandler
from decorators import is_ajax
class MyHandler(RequestHandler):
@is_ajax # is_ajax decorators.
def get(self):
self.write("ok!")
Upvotes: 2