2

I have a simple AppEngine application that has one file named WebInterface.py responsible or routing the user to the correct pages with the following code:

import webapp2
from handlers import MainHandler
from handlers import PageOneHandler
from handlers import PageTwoHandler

app = webapp2.WSGIApplication([
    ('/', MainHandler),
    ('/pageOne', PageOneHandler),
    ('/pageTwo', PageTwoHandler)
], debug=True)

And another handlers.py that contains all the RequestHandlers responsible of displaying pages to the user. It has the following code:

import webapp2

class MainHandler(webapp2.RequestHandler):
    def get(self):
        self.response.write('Hello, You are in the Main Page!')

class PageOneHandler(webapp2.RequestHandler):
    def get(self):
        self.response.write('Hello, You are in the First Page!')
        self.response.write('<br><html><head><script     src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script></head>    <body>')
        self.response.write('<button id="btn" onClick="doAction()">Click Me')
        self.response.write('</button><p id="demo"></p>')
        self.response.write('<script>')
        self.response.write('function doAction()')
        self.response.write('{document.getElementById("demo").innerHTML="Hello World";')
        self.response.write('var data = {')
        self.response.write('user : "UserName", pwd : "Password"')
        self.response.write('};')
        self.response.write('jQuery.post("/pageOne", data);')
        self.response.write('}')
        self.response.write('</script>')
        self.response.write('</body></html>')

    def post(self):
        self.redirect('/pageTwo')

class PageTwoHandler(webapp2.RequestHandler):
    def get(self):
        self.response.write('Hello, You are in the Second Page! Get method')
    def post(self):
        self.response.write('Hello, You are in the Second Page! Post method')

Well, when I try to access one of the three pages I get the expected results, for example if I type in my browser this address: http://localhost:13080/ I get the Main page as a result, If I enter this address: http://localhost:13080/pageOne I get the first page and if I enter this address: http://localhost:13080/pageTwo I get the second page as expected.

Now, my problem is that when clicking on the Click Me button in the first page, The HTTP POST request is sent and PageOneHandler's post method is executed, which is supposed to redirect me to the pageTwo and it does! We can see the proof in the Console:

INFO     2014-03-13 10:36:47,831 module.py:612] default: "GET /pageOne HTTP/1.1" 200 405
INFO     2014-03-13 10:37:08,606 module.py:612] default: "GET /pageOne HTTP/1.1" 200 409
INFO     2014-03-13 10:37:32,065 module.py:612] default: "POST /pageOne HTTP/1.1" 302 -
INFO     2014-03-13 10:37:32,090 module.py:612] default: "GET /pageTwo HTTP/1.1" 200 45

But I am still in the same page (/pageOne). So why I didn't get the text contained in pageTwo's get method ?

Thank you.

laribiwalid
  • 148
  • 9

1 Answers1

0

The short answer is that it will be redirected if you issue a POST request using your browser and not with a jQuery.post(). For example if you have a <form action="/pageOne" method="post">... and submit this without using JavaScript you'll see that it will eventually take you to the pageTwo, but when you're doing it with JS it is not changing the location automatically.

If you want to try your approach anyway, then read more on How to manage a redirect request after a jQuery Ajax call.

Community
  • 1
  • 1
Lipis
  • 21,388
  • 20
  • 94
  • 121
  • Thank you for your prompt answer. Well the code in this question was just to illustrate and simplify the problem, I'm obviously using templates. Well it will actually redirected, you can see this in the Log console. But the page is just not being changed. Should I use a `
    ` every time I want to pass data from my html to python code ? Why can't I just post the data via Javascript ? I really don't understand the reason for this. Thanks.
    – laribiwalid Mar 13 '14 at 11:10
  • You can POST via JS and it is being redirected.. but not in your browser.. if you want to do that with what you already trying.. then read the answers here: http://stackoverflow.com/q/199099/8418 – Lipis Mar 13 '14 at 11:31