[英]How would I transfer information like a variable from javascript to python using AJAX and Flask
I am messing around with Flask and Ajax to see what I can do with it for a bigger project I'm working on.我正在使用 Flask 和 Ajax,看看我可以用它为我正在从事的更大项目做些什么。 I am trying to get some data in a variable into my python program to be able to use and manipulate.
我试图将变量中的一些数据放入我的 python 程序中,以便能够使用和操作。 I have a script in an html file that returns the current URL.
我在返回当前 URL 的 html 文件中有一个脚本。 How can I get the url into my python program with AJAX?
如何使用 AJAX 将 url 输入到我的 python 程序中?
I will attach my relevant code below:我将在下面附上我的相关代码:
index.html索引.html
<html lang="en">
<head>
<meta charset="UTF-8">
<title>HELP</title>
</head>
<body>
<p>I am {{person}} and i am {{age}} years old, I am a {{ql}}</p>
<form name="passdata" action="." method="post">
<label>Name:</label>
<input type="text" name="name">
<label>Age:</label>
<input type="text" name="age">
<label>Date of Birth:</label>
<input type="text" name="dateofbirth">
<input type="submit" value="submit">
</form>
<p id="demo"></p>
<script>
let geturl = window.
document.getElementById("demo").innerHTML =
"The full URL of this page is:<br>" + geturl;
</script> //getting the current url
</body>
</html>
main.py主文件
from flask import Flask, render_template, request
app = Flask(__name__)
name = "random"
age = "21"
qualification = "software engineer"
@app.route('/')
def index():
return render_template('index.html', person=name, age=age, ql=qualification)
@app.route('/', methods=['POST'])
def getvalue():
name2 = request.form['name']
age = request.form['age']
db = request.form['dateofbirth']
return name2
if __name__ == '__main__':
app.run(debug=True)
let me know if you need any clarification.如果您需要任何说明,请告诉我。 Thanks in advance.
提前致谢。
Usually when you are sending variables to routes in flask you will have the route designed as:通常,当您将变量发送到 Flask 中的路由时,您的路由将设计为:
@app.route('/route_to_send_to/<variable>', methods=['GET'])
def route_to_send_to(variable):
variable_sent = variable
return jsonify({'variable':variable})
Another hint usually when working with ajax calls the response may be a json response which you can send back with jsonify.另一个提示通常在使用 ajax 调用时,响应可能是一个 json 响应,您可以使用 jsonify 将其发回。
All you need to do is call it to the route replacing with the variable you want to send.您需要做的就是将其调用到路由替换为您要发送的变量。
You can also send with Ajax a post request to have the data hidden, but then you will need to change the route to handle post requests.你也可以用 Ajax 发送一个 post 请求来隐藏数据,但是你需要更改路由来处理 post 请求。
The Ajax call may be something like: Ajax 调用可能类似于:
var url = '/route_to_send_to/ + name;
return $.ajax({
type: "GET",
url: url,
}).then(function(data) {// Do something with the responce})
});
Considering the comments below I might suggest you read this article to get a better understanding of how flask and ajax work together.考虑到下面的评论,我可能建议您阅读这篇文章,以更好地了解flask 和ajax 如何协同工作。
Also This amazing Flask tutorial by Miguel Grinberg is probably one of the best resources I have ever come across for learning flask, the framework and good conducts with it.此外,这个由 Miguel Grinberg 编写的令人惊叹的Flask 教程可能是我遇到的学习Flask 、框架和良好行为的最佳资源之一。 It goes through everything from the absolute basic to extremely advanced topics and is well worth the time.
它涵盖了从绝对基础到极其高级的主题的所有内容,非常值得花时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.