没有重定向的 Flash 消息弹出
Flash Message pop up without redirect
我正在尝试找到一种无需重定向页面即可让 Flask 闪烁消息弹出的方法。基本上,如果您输入一些匹配的助记符模式,它会搜索数据库并将其显示在搜索栏下方的警报中。
我试过完全删除重定向,但这使它无法正常工作。
javascript 将用户带到 URL:
function search() {
var searchString = $("#inputSearch").val();
if (searchString.length == 4 && searchString.match(mnemonicPattern)) {
$.get("search/" + searchString, function () {
goToURL("search/"+searchString);
labCatalog.search(searchString, true, false).draw();
});
}
else if (searchString.length >= 6 && searchString.match(oracleNumberPattern)) {
$.get("search/" + searchString, function (data) {
goToURL("search/"+searchString);
labCatalog.search(searchString, true, false).draw();
});
}
else {
labCatalog.search(searchString).draw();
}
}
从数据库中获取数据的搜索函数:
@app.route('/search/<string:id_data>',methods=['GET'])
def search(id_data):
cur = mysql.connection.cursor()
cur.execute("SELECT * FROM sims WHERE mnemonic=%s", [id_data])
result = cur.fetchall()
cur.close()
for match in result:
message = "Mnemonic: " + str([match[0]]) + ' Description:
+ str([match[1]]) + ' RFT Date: ' + str([match[2]])
flash(message)
return redirect(url_for('Index'))
我只想弹出即显消息而无需使用 localhost/search/mnemonicpattern 重定向。
您需要进行两项更改 - 一项在客户端显示警告框,另一项在服务器端显示 return 数据。
1) 在客户端更改
goToURL("search/"+searchString);
到
alert(data)
2) 在服务器端更改
message = "Mnemonic: " + str([match[0]]) + ' Description:
+ str([match[1]]) + ' RFT Date: ' + str([match[2]])
flash(message)
return redirect(url_for('Index'))
至
message = "Mnemonic: " + str([match[0]]) + ' Description: '
+ str([match[1]]) + ' RFT Date: ' + str([match[2]])
return message
如果结果中有多个匹配项,上述更改只会发送最后一个匹配项。如果您想将所有匹配项组合在一个字符串中以发送给客户端以显示在警报中,您可能需要使用“消息 +=” 而不是 "message ="
我正在尝试找到一种无需重定向页面即可让 Flask 闪烁消息弹出的方法。基本上,如果您输入一些匹配的助记符模式,它会搜索数据库并将其显示在搜索栏下方的警报中。
我试过完全删除重定向,但这使它无法正常工作。
javascript 将用户带到 URL:
function search() {
var searchString = $("#inputSearch").val();
if (searchString.length == 4 && searchString.match(mnemonicPattern)) {
$.get("search/" + searchString, function () {
goToURL("search/"+searchString);
labCatalog.search(searchString, true, false).draw();
});
}
else if (searchString.length >= 6 && searchString.match(oracleNumberPattern)) {
$.get("search/" + searchString, function (data) {
goToURL("search/"+searchString);
labCatalog.search(searchString, true, false).draw();
});
}
else {
labCatalog.search(searchString).draw();
}
}
从数据库中获取数据的搜索函数:
@app.route('/search/<string:id_data>',methods=['GET'])
def search(id_data):
cur = mysql.connection.cursor()
cur.execute("SELECT * FROM sims WHERE mnemonic=%s", [id_data])
result = cur.fetchall()
cur.close()
for match in result:
message = "Mnemonic: " + str([match[0]]) + ' Description:
+ str([match[1]]) + ' RFT Date: ' + str([match[2]])
flash(message)
return redirect(url_for('Index'))
我只想弹出即显消息而无需使用 localhost/search/mnemonicpattern 重定向。
您需要进行两项更改 - 一项在客户端显示警告框,另一项在服务器端显示 return 数据。
1) 在客户端更改
goToURL("search/"+searchString);
到
alert(data)
2) 在服务器端更改
message = "Mnemonic: " + str([match[0]]) + ' Description:
+ str([match[1]]) + ' RFT Date: ' + str([match[2]])
flash(message)
return redirect(url_for('Index'))
至
message = "Mnemonic: " + str([match[0]]) + ' Description: '
+ str([match[1]]) + ' RFT Date: ' + str([match[2]])
return message
如果结果中有多个匹配项,上述更改只会发送最后一个匹配项。如果您想将所有匹配项组合在一个字符串中以发送给客户端以显示在警报中,您可能需要使用“消息 +=” 而不是 "message ="