简体   繁体   English

我如何使用 AJAX 和 Flask 将诸如变量之类的信息从 javascript 传输到 python

[英]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.

相关问题 如何将变量从 Python Flask 转移到 JavaScript? - How can I transfer a variable from Python Flask to JavaScript? 如何使用JavaScript(nodejs)中的回调从函数将JSON信息转化为变量,例如? - How would I get JSON information into, say, a variable, from a function using a callback in Javascript (nodejs)? 如何从 python Flask 接收变量到 JavaScript? - How do I receive a variable from python flask to JavaScript? 我想从ajax加载页面执行javascript - I would like to execute javascript from ajax loaded page 我如何使用flask从javascript获取变量 - how can I get variable from javascript using flask 我如何将 JavaScript 参数传递到 flask 中的 python 代码中? - How would I pass a JavaScript parameter into my python code in flask? 如何将变量设置为等于来自 API 的信息? (Js) - How would I set a variable to be equal to information from an API? (Js) 如何转移python对象以在JavaScript变量中使用? - How can I transfer a python object for use in a JavaScript variable? 如何在没有Web服务器的情况下将变量数据从Python传输到Javascript? - How to transfer variable data from Python to Javascript without a web server? 如何将带有嵌入式对象的对象从JavaScript(jQuery / ajax)传递给Python(Flask)? - How can I pass an object with an embedded object from JavaScript (jQuery/ajax) to Python (Flask)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM