close

How do I send data from JS to Python with Flask?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How do I send data from JS to Python with Flask in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How do I send data from JS to Python with Flask?

  1. How do I send data from JS to Python with Flask?

    .html
    ... id="clickMe" onclick="doFunction();">
    .js
    function doFunction() { const name = document.getElementById("name_").innerHTML

  2. send data from JS to Python with Flask

    .html
    ... id="clickMe" onclick="doFunction();">
    .js
    function doFunction() { const name = document.getElementById("name_").innerHTML

Method 1

.html

... id="clickMe" onclick="doFunction();">

.js

    function doFunction()
    {
        const name = document.getElementById("name_").innerHTML

        $.ajax({
            url: '{{ url_for('view.path') }}',
            type: 'POST',
            data: {
                name: name
            },
            success: function (response) {
            },
            error: function (response) {
            }
        });

    };

.py

@app.route("path", methods=['GET', 'POST'])
def view():
    name = request.form.get('name')
    ...

Conclusion

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read