【问题标题】:Flask cookie not set with ajax request未使用 ajax 请求设置 Flask cookie
【发布时间】:2017-12-04 14:04:11
【问题描述】:

我正在尝试在来自 React 应用程序的 ajax 请求上设置来自 Flask 服务器的 cookie。我做了一个简单的测试用例,即使这样,cookie 在响应中发送,但没有在浏览器中设置

烧瓶:

@app.route('/is_alive', methods = ['POST'])
def alive():
    resp = app.make_response("There should be a cookie")
    resp.set_cookie("testing", 'testing')
    return resp

Javascript:

static isAlivePost() {
    return new Promise(resolve => {
        let options = {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'Accept': 'application/json',
            },
            credentials: 'same-origin',
            body: JSON.stringify({
                firstParam: 'true'
            })
        }

        fetch(endpoints.MOCK_API + 'is_alive', options)
            .then(response => {
                resolve(response.text());
            }).then(function(data) {
                resolve(data);
            })
            .catch(e => {
                reject(e);
            });
    });
}

【问题讨论】:

    标签: javascript python ajax cookies flask


    【解决方案1】:

    credentials: 'same-origin' 必须是 credentials: 'include'

    【讨论】:

      猜你喜欢
      • 2014-02-08
      • 2015-12-21
      • 2021-12-28
      • 2017-08-05
      • 2016-06-19
      • 1970-01-01
      • 1970-01-01
      • 2016-07-21
      相关资源
      最近更新 更多