user1830238
user1830238

Reputation:

How to run ajax request handler tornado

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

Answers (2)

Zhang LongQI
Zhang LongQI

Reputation: 494

check out this: this one should be more easier to understand:

https://github.com/zhanglongqi/TornadoAJAXSample

Upvotes: 1

yasaricli
yasaricli

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

Related Questions