gpt4 book ai didi

javascript - 如何将 Javascript 输入的数据发送到 Flask

转载 作者:行者123 更新时间:2023-12-01 01:09:54 27 4
gpt4 key购买 nike

我有一个表格,每行都有按钮。如果我单击某个按钮(例如第 3 行),第 3 行的 ID 将同时显示在警报和 console.log 中。我最终想将行id发送到flask python,然后该id号可用于删除或复制有问题的行。

我需要使用 Ajax post,它与下面链接中的示例配合得很好。 https://github.com/bsaldivaremc2/send_receive_data_with_flask

当我修改现有代码以合并上面链接中示例中的 ajax post 时,我不断在命令终端和控制台检查中收到此错误。

命令终端:

类型错误:“int”对象不可调用 View 函数未返回有效响应。返回类型必须是字符串、元组、Response 实例或可调用的 WSGI,但它是 int。

控制台检查:发布http://127.0.0.1:5000/delete_data 500(内部服务器错误)

这是我的 HTML/JS

test2.html

<!DOCTYPE html>
<html>

<head>

<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title></title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="robots" content="noindex, nofollow">
<meta name="googlebot" content="noindex, nofollow">
<meta name="viewport" content="width=device-width, initial-scale=1">

<meta name="viewport" content="width=device-width, initial-scale=1">

<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<script src="http://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js" integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU=" crossorigin="anonymous"></script>


<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>

<script>
type="text/javascript"
src="//code.jquery.com/jquery-1.9.1.js"
</script>

<title>Test 1</title>

<style>


table, th, td {
border: 1px solid black;
border-collapse: collapse;
}



</style>

</head>
<body>




<table id="table-test">
<tr><th>ID</th><th>Student</th><th>Delete</th></tr>

<tr><td class="ids">1</td><td>Chelsey</td><td><button id="Approved">Del</button></td></tr>

<tr><td class="ids">2</td><td>Michael</td><td><button id="Approved">Del</button></td></tr>
<tr><td class="ids">3</td><td>Jared</td><td><button id="Approved">Del</button></td></tr>
</table>



<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="http://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js" integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU=" crossorigin="anonymous"></script>



<script type="text/javascript">




$(document).ready(function(){
$(document).on("click","#Approved",function(){
var info = $(this).parent().siblings('.ids').text();
var all_info = $(this).parent().siblings("#table-test");

alert(info);

console.log("Del button clicked!");


var num = info.trim().toString();

console.log(num);
console.log(typeof(num));

var row = {"id": num};

row["id"] = num;

console.log(typeof(num));

var server = "http://127.0.0.1:5000";
var appdir = "/delete_data";

$.ajax({

type:"POST",
url: server+ appdir,
data: JSON.stringify(info),
dataType: "json",
contentType: "application/json;charset=UTF-8",
success: function(res){

console.log(res);
}
})


});

});


</script>

</body>
</html>

我的 Flask 我正在尝试将行 ID 号发送到 delete_data View 函数。

from flask import render_template, jsonify, Flask
import json

app = Flask(__name__)

@app.route("/delete_data", methods = ["GET", "POST"])
def delete_data():


print("Deleting selected row!")
rf = request.form
print(rf)


for key in rf.keys():


data = key


print("DATA: ", data)

data_dic = json.loads(data)

id_data = data_dic["id"]

print(data_dic)
print(id_data)


print(type(data_dic))
print(type(id_data))


return id_data





@app.route("/test2", methods = ["GET", "POST"])
def test2():


return render_template("test2.html")


if __name__ == "__main__":

app.run(debug=True)

任何帮助将不胜感激。谢谢!

最佳答案

错误消息几乎说明了一切。问题很可能出在:

id_data = data_dic["id"]
...
return id_data

您正在返回 id_data,它是一个 int,但您需要返回错误消息中指定的任一类型。尝试这样:

return jsonify({'id': id_data})

编辑:回答评论中的后续问题。打印 request.form (作为 rf 变量)时,您会得到一个空字典,因为您实际上没有将表单数据发布到 View 函数,而是在 POST 中发送 JSON 正文要求。要获取数据,您必须使用 request.get_json() 而不是 request.form

关于javascript - 如何将 Javascript 输入的数据发送到 Flask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55258572/

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