Why non existing URL containing the sharp (“#”) si

2019-07-04 21:35发布

I'm building a Python/Flask (fully ajax, without full page reloading) web application and I change the content of my master page when the sharp ("#") sign on the url change (Ben Alman, HashChange event)

I would like to catch all Error-404 to append an error page in my master page

Taking the following code in consideration

from flask import Flask, redirect

#WebServerGatewayInterface
class WSGI():
    def __init__(self):
        self.app = Flask(__name__)
        self.app.debug = True


wsgi = WSGI()

@wsgi.app.route("/")
def Home():
    return "Home"

@wsgi.app.route("/Contact")
def Contact():
    return "Contact"

@wsgi.app.errorhandler(404)
def PageNotFound(error):
    return "PageNotFound"

if __name__ == '__main__':
    wsgi.app.run(host='127.0.0.1', port=7777)

I would like to understand why when I add a non-exising URL after the sharp ("#") sign, it isn't trapped by the errorhandler ?

E.g.

127.0.0.1:7777
#It returns "Home"

127.0.0.1:7777/Contact
#It returns "Contact"

127.0.0.1:7777/Aasdasdasdasd
#It returns "PageNotFound"

127.0.0.1:7777/#/asdsaasdasdasdas
#It returns "Home", which is not right since this URL doesn't exist

127.0.0.1:7777/#!/asdsaasdasdasdas
#It returns "Home", not ok...

127.0.0.1:7777/#!/Contact
#It returns "Home", not ok...

1条回答
一夜七次
2楼-- · 2019-07-04 21:55

Everything after the # forms the fragment identifier of a resource and is handled client side. In the normal course of operation, the fragment identifier is never sent to the server:

The fragment identifier functions differently than the rest of the URI: namely, its processing is exclusively client-side with no participation from the web server

If a server is ever sent a fragment identifier in a URL, it must ignore this part of the URL. It is not part of the path, and thus Flask just sees 127.0.0.1:7777/ for all your sample URLs with a fragment.

查看更多
登录 后发表回答