gpt4 book ai didi

jquery - JQuery 和 Flask 没有返回任何内容?

转载 作者:行者123 更新时间:2023-11-29 23:28:25 25 4
gpt4 key购买 nike

我正在编写一些代码,基本上使用 mysql、jquery 和flask 制作用户名和密码框。目前我的 mysql 数据库 100% 正常工作,但 Flask 不会将值返回到 JQuery,因此我无法更新 html 中的 div。编辑:使用console.log有一个错误,只是“”

这是我目前在 Flask 中遇到问题的方法

@app.route("/authentication/")
def validation():
username = {"value": request.args.get('echoValue')}
password = {"value": request.args.get('echoValue2')}
cursor = mysql.connect().cursor()
cursor.execute("SELECT * from _accounts where Username='" + str(username['value']) + "' and Password='" + str(password['value']) + "'")
data = cursor.fetchone()
if data is None:
return "Username or Password is wrong"
else:
return "Logged in successfully"

这是尝试输出对 div 结果的响应的 JQuery:

<script type=text/javascript>
$(function() {
$("#sign-in").click(function() {
$.ajax({
type: "GET",
url: "/authentication/",
contentType: "application/xml; charset=utf-8",
data: { echoValue: $('input[name="username"]').val(), echoValue2: $('input[name="password"]').val() },
success: function(data) {
alert("working")
alert(data)
$('#results').text(data.value);
}
});
});
});
</script>

最佳答案

尝试返回 json.dumps({"value":string})

并在另一边尝试使用相同的data.value访问它

您可能需要先解析它

var json = JSON.parse(data)`

编辑:从flask导入json

关于jquery - JQuery 和 Flask 没有返回任何内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26770615/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com