Я хочу нажать на href и выполнить функцию javascript, чтобы опубликовать какое-то значение в python и отобразить новый шаблон с этими данными, это мой код.
index. html
<!DOCTYPE html>
<html lang="en">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<body>
<a href="#" onclick="myFunction();">Click</a>
<script>
var jsvariable = 'hello world'
function myFunction(){
$.ajax({
url: "/getvalue",
type: "post", //send it through get method
data:{'jsvalue': jsvariable},
})
}
</script>
</body>
</html>
server.py
from flask import Flask, render_template, request, url_for
app = Flask(__name__)
@app.route('/', methods=['GET','POST'])
def index():
return render_template('index.html')
@app.route('/getvalue', methods=['GET','POST'])
def getvalue():
data = request.form['jsvalue']
print(data)
return render_template('index2.html', data=data)
if __name__ == "__main__":
app.run(debug=True)
Теперь данные, которые передаются из ajax, выполняют функцию getvalue () в python, которая работает правильно, но не отображается в новом шаблоне, так как это сделать? я исправляю это.