【问题标题】:Couldn't set cookie with flask无法用烧瓶设置 cookie
【发布时间】:2019-07-29 21:54:38
【问题描述】:

对于开发,我正在使用由 webpack 在我的本地地址提供服务的 vuejs:172.18.0.77:8080 和由 Werkzeug 在地址172.18.0.77:5000 运行的烧瓶。我正在尝试通过运行以下代码将 cookie 设置为一些 GET 请求:

response_data = Response(json.dumps(some_json_data, indent=True), status=200, mimetype='application/json')
response_data.set_cookie('user_session_id', value='12345', domain='172.18.0.77:8080')
return response_data

但是当我尝试使用以下代码读取此 cookie 时 request.cookies.get('user_session_id') 我只收到 None 值。

我还尝试通过将域更改为 172.18.0.77 来设置 cookie,例如:

 response_data.set_cookie('user_session_id', value='12345', domain='172.18.0.77')

但它也不起作用

【问题讨论】:

  • @asergio 感谢您的回复!我只是尝试使用主机文件迁移到本地域,并使用此代码 response_data.set_cookie('user_session_id', value='12345', domain='domain.local:8080')response_data.set_cookie('user_session_id', value='12345', domain='domain.local') 设置 cookie,但烧瓶仍然无法读取此 cookie。顺便说一句,我可以在标题中看到苦力。

标签: python vue.js cookies flask session-cookies


【解决方案1】:

如果你在vuejs中使用axios,我建议你可以加withCredentials: true

const instance = axios.create({
    withCredentials: true,
    ....
})

在烧瓶中

@app.after_request
def handle_credentials(response):
    response.headers["Access-Control-Allow-Credentials"] = True
    return response

【讨论】:

    猜你喜欢
    • 2012-07-31
    • 1970-01-01
    • 1970-01-01
    • 2014-01-12
    • 1970-01-01
    • 1970-01-01
    • 2018-10-07
    • 1970-01-01
    • 2012-12-27
    相关资源
    最近更新 更多