Skip to content
Advertisement

How I can send data from Flask to JavaScript?

Hello I am new and building an application in Flask and Javascript. I have a problem with sending data from Flask do JavaScript.

I have code in routes.py

JavaScript

I want to send jsonify(slownik) to my code in JavaScript but I dont want to do it with render_template bacause it refresh a page and I need this data to display it on the map using JavaScript. I tried return jsonify(slownik) but it return me the data on new page. How I can send the data when the user click event_form button to JavaScript without refreshing page?

Advertisement

Answer

You can use ajax for sending post request without refreshing the page. Note- include jquery before this

JavaScript

javascript code

JavaScript

make sure you don’t use form tag.

Edit: If you want to send more data via forms, you could use

JavaScript

this will take the name attribute of input tag and add it in the request.data So if you have this

JavaScript

You can use the value of name attribute like this request.data['username'] follow this tutorial

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement