我试图在来自反应应用程序的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);
});
});
}
答案 0 :(得分:1)
credentials: 'same-origin'
需要credentials: 'include'
。