我想将每个具有值的键发布到 Flask 框架中的函数中,以将它们存储在 MySQL 数据库中。我可以这样做吗?
jQuery
$.ajax({
type: "POST",
url: "{{url_for('savedata')}}", // Function to send data
data: sessionStorage.serializeArray(),
success: function(data) {
window.location.replace("/quit"); // Redirect to another page after saving data
}
});
应用程序.py
conn = mysql.connect()
cursor = conn.cursor()
@app.route("/savedata",methods=["POST"])
def savedata():
sql = "INSERT INTO data_table "
data = ""
for (var i = 0; i < sessionStorage.length; i++) {
var item = sessionStorage.getItem(sessionStorage.key(i));
data += "item " + "VARCHAR(255)"
}
sql += data
cursor.execute(sql)