Skip to content Skip to sidebar Skip to footer

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 @app.route('/mapaa',method

Solution 1:

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

<scriptsrc="https://code.jquery.com/jquery-3.1.0.js" ></script>

javascript code

$("#id_of_btn").click(function () { // make ajax request on btn click
  $.ajax({
    type: "POST",
    url: "/mapaa", // url to the functiondata: {
      name: $("#id_of_input_tag").val(), // value of the form
    },
    success: function (response) {
      console.log(response); // response contains the json
    },
  });
});

make sure you don't use form tag.

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

data: $('form').serialize()

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

<inputtype="text"id="element_id" name="username" > 

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

Post a Comment for "How I Can Send Data From Flask To Javascript?"