0


I am writing a website that has to display the IP and Location of 10 most recent users and store all the visits in a database. The website has to be coded in Flask and HTML/CSS/JS. I have already written a script that does ipLookup() in my .js file, but now I am having trouble with passing the JS data into my python flask app so that I can add it to my database.

Overall, how do I pass JS script output into Python Flask?
TIA

EDIT: thank you for your quick responses they really helped and guided me to a solution. For future ref I recommend this article tho: https://scotch.io/bar-talk/processing-incoming-request-data-in-flask

1 Answer 1

2

You'd POST the data from your JavaScript code to a Flask view, which can then do things with it. Using the Fetch API,

const myIp = ipLookup();
fetch('/save-ip', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({ip: myIp}),
}).then(r => {
  console.log(r);
}).catch(e => {
  alert('Error: ' + e);
});

and something like

@app.post("/save-ip")
def save_ip():
  data = request.get_json())
  print(data)
  # ... save ...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.