返回重定向时请求不断重复。
我制作了一个按钮来发送删除请求,它将使用键“order_list”删除会话数据,但它会继续进行重定向,直到浏览器停止为止
cart_order.html
<script>
function clearQuery() {
fetch("/cart", {
method: "DELETE",
headers: {
"Content-Type": "application/json",
},
})
.then((response) => {
if (response.ok) {
return response.json();
}
throw new Error("Network response was not ok.");
})
.then((data) => {
console.log("All orders have been deleted");
})
.catch((error) => {
console.error("Error deleting orders", error);
});
}
</script>
<button onclick="clearQuery()">Clear Query</button>
烧瓶
from flask import Flask, render_template, redirect, url_for, session, request
app = Flask(__name__)
app.secret_key = 'THIS_IS_SECRET_KEY'
@app.route('/cart', methods=["GET", "DELETE"])
def cart():
# for demonstration
session['order_list'] = {
'order_1': {
'marker': True,
'color': 'blue'
},
'order_2': {
'marker': True,
'color': 'red'
},
'order_3': {
'marker': True,
'color': 'blue'
}
}
if request.method == "GET":
if 'order_list' in session:
print(session['order_list'])
else:
print(session)
return render_template('cart_order.html')
elif request.method == "DELETE":
session.pop('order_list')
session.modified = True
return redirect(url_for('cart'))
if __name__ == "__main__":
app.run(debug=True)
我希望删除带有“order_list”键的会话数据,然后使用 GET 请求重定向回自身,并打印现在应该为空的会话