0

I'm new using flask or JS, as that, I can't find a way to do what I want to. I created a webserver using flask (in python) and it uses index.html as main page, I wanted to update data to the server every few secounds (maybe 1-3 secs). The thing is, I don't have any form to work with, or even queries and I don't know what else can I work with. The data I want to send is small string to be saved on server host later on.

<body>
    <center>
        <button onmousedown="sendDirectionKey('^')">^</button>
        ...
    </center>
</body>

<script>
    function sendDirectionKey(Key) 
    {
        ...
        sendData(data_string);
    }

    function sendData(data)
    {
        ...
    }
</script>
Diogo Dias
  • 15
  • 8

1 Answers1

1

An easy modern solution is the fetch() API:

function sendData(data)
{
  const body = new FormData();
  body.append("key", data);
  return fetch("/receive", {method: "POST", body, credentials: "include"});
}

The equivalent receiver on the Python side would be something like

@app.route("/receive", methods=["POST"])
def receive():
    print(request.form)  # should have a `key` key
AKX
  • 152,115
  • 15
  • 115
  • 172