在Python Flas中传递Javascript数组

2024-06-10 17:04:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用python flask开发和应用程序,我想将Javascript函数(数组)的返回值发送给python函数。下面是代码。在

Javascript

<script>
  function getVals(){
      a = [ab, cd];
      return a;
  }
</script>

HTML

^{pr2}$

Python

@app.route('/end_stu_live_session')
def end_stu_live_session():
   return render_template('stu_dashboard.html')

我希望Javascript函数getVals()中的数组传递给python函数end_stu_live_session。我们非常感谢您的帮助。谢谢。在


Tags: 函数代码live应用程序flaskreturnsessionscript
1条回答
网友
1楼 · 发布于 2024-06-10 17:04:09
  • 可以通过Ajax将Javascript函数(数组)的返回值作为JSON发送给Python方法。在
  • 从AJAX发送contentType: 'application/json',并以request.json的形式接收数据。在

应用程序副本:

from flask import Flask, render_template, request, jsonify

app = Flask(__name__)

@app.route('/end_stu_live_session',methods=["GET", "POST"])
def end_stu_live_session():
    if request.method == 'POST':
        data = request.json
        return jsonify(data)
    return render_template("home.html")

app.run(debug=True)

主页.html:

^{pr2}$

输出:

{a1}

相关问题 更多 >