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> 
0

1 Answer 1

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 
Sign up to request clarification or add additional context in comments.

4 Comments

It doesn't show anything new now, where can I see those new data values?
You'll need to do something with the received data, of course.
I know that ahah, tho there must be some error on sendData(), because it gives me this on console, and doesn't reach the server host. "Uncaught TypeError: body.add is not a function"
Just fixed all with .append() instead. Much thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.