Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery post url parameters comparison with python not working

Iam using jquery AJAX request using post methid to send parameters to tornado server and did something like this:

$.ajax({
    type: "POST",
    url: "/leave_action?id=" + rowid.id + "&action=" + param,
    dataType: "json",
    async: false
  })

But when I'm comparing using python, it is not working:

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"

I tried with is also but no luck.

I also tried with:

      action = self.get_query_argument("action")
      a=action.encode('ascii','ignore')

but no use.

like image 981
Kranthi Sama Avatar asked Feb 04 '26 02:02

Kranthi Sama


1 Answers

There is some points you better to know ! The Tornado is a MVC based framework that let you have a lot of things in your hand.

So, you can set URL pattern instead using query string. that's mean you don't need to get value with self.get_query_argument() or self.get_argument().

For URL:

application = tornado.web.Application([
    (r'^(?i)/leave_action/([\w+^/])/([\w+^/])[/]?$', YourLeaveActionHandler)
], **settings)

or even:

application = tornado.web.Application([
    (r'/leave_action/(id)/(approve|decline)', YourLeaveActionHandler, None, "this_url_name")
], **settings)

The second URL pattern is better a bit. because we emphasize expressly the first group should be id and the second group should approve OR decline.

And in your handler:

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"

In your handler , the action should be approve OR decline and anything else expect these not accepted in this handler. So you can easily compare the action value.

  • And you should note this,sometimes when you using POST method, the query string maybe ignored by tornado.
like image 121
Reza-S4 Avatar answered Feb 05 '26 18:02

Reza-S4