Python-使用Flask获取URL片段标识符 [英] Python - Get URL fragment identifier with Flask

查看:110
本文介绍了Python-使用Flask获取URL片段标识符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Flask程序在#之后收到以下请求以及一些数据:

My Flask program receives the following request with some data after the #:

https://som.thing.com/callback#data1=XXX&data2=YYY&data3=...

我需要阅读data1参数,但这似乎不起作用:

And I need to read the data1 parameter, but this doesn't seem to work:

@app.route("/callback")
def get_data():
    data = request.args.get("data1")
    print(data)

推荐答案

URL的哈希(#之后的所有内容)从不会发送到服务器,浏览器会删除它,保留URL的那部分完全在客户端.根据维基百科:

The hash of the URL (everything after the #) is never sent to the server, the browser will strip it away, keeping that part of the URL completely client-side. According to Wikipedia:

片段标识符的功能与URI的其余部分不同:它的处理是客户端专用的,没有来自Web服务器的参与,[...].当代理(例如Web浏览器)从Web服务器请求Web资源时,代理将URI发送到服务器,但不发送片段.

The fragment identifier functions differently to the rest of the URI: its processing is exclusively client-side with no participation from the web server, [...]. When an agent (such as a Web browser) requests a web resource from a Web server, the agent sends the URI to the server, but does not send the fragment.

这意味着无论您使用哪种框架,都无法在后端检索它,因为它们都不会收到该数据.

This means there's no way to retrieve it on the backend, no matter which framework you use, as none of them will ever receive that piece of data.

您需要改用查询参数,因此您的网址应如下所示:

You need to use query parameters instead, so your URL should look like this:

https://foo.com/bar?data1=ABC&data2=XYZ

在这种情况下,您将可以使用 request.args :

And in this case, you will be able to access them using request.args:

from flask import request

@app.route('/bar')
def bar():
    page = request.args.get('data1', default = '', type = str)
    filter = request.args.get('data2', default = 0, type = int)

这篇关于Python-使用Flask获取URL片段标识符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆