Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to GET data in Flask from AJAX post

I want to retrieve the data from the variable 'clicked' so I can use it in SQL queries in Flask.

JQuery

$(document).ready(function(){
  var clicked;
  $(".favorite").click(function(){
    clicked = $(this).attr("name");
    $.ajax({
      type : 'POST',
      url : "{{url_for('test')}}",
      data : clicked
    });
  });
});

Flask/Python

@app.route('/test/', methods=['GET','POST'])
def test():
    return render_template('test.html')
like image 464
0248881 Avatar asked Jun 04 '16 14:06

0248881


People also ask

How do you transfer data to a flask?

Python for web development using FlaskThe Form data received by the triggered function can collect it in the form of a dictionary object and forward it to a template to render it on a corresponding web page. In the following example, '/' URL renders a web page (student.

What is AJAX in flask?

jQuery is a small JavaScript library commonly used to simplify working with the DOM and JavaScript in general. It is the perfect tool to make web applications more dynamic by exchanging JSON between server and client.

How to post form data to flask form using jQuery Ajax?

Now we’ll use jQuery AJAX to post the form data to the Python Flask method. Create a new JavaScript file called script.js_and include it after jQuery in _signUp.html. We’ll attach a click event on button click and call python method using jQuery AJAX. Here is how the script.js looks like:

How to GET POST data in flask?

Now coming to the exciting part of getting POST data in Flask. The very first way of getting POST data is using Form data. this form data is retrieved from a form that is sent as a POST request to a route. In this scenario, the URL doesn’t see the data, and it gets passed to the app from the forms behind the scenes.

Why Python Flask jQuery Ajax?

Why Python Flask jQuery AJAX ? Why do we need to use jQuery AJAX along with a python web application ? When creating a web application it’s often needed to pass in the data from client side to the server side methods to get response from the server. jQuery is just one simple way to achieve this.

How to send data from HTML to flask route?

Let’s take a simple HTML file having two input fields. The user will enter the first name and last name and the data will be sent to the flask route as Post request and it will display the value of the field in the HTML.


2 Answers

You can compose your payload in your ajax request as so:

$(document).ready(function(){
var clicked;
$(".favorite").click(function(){
clicked = $(this).attr("name");
$.ajax({
  type : 'POST',
  url : "{{url_for('test')}}",
  contentType: 'application/json;charset=UTF-8',
  data : {'data':clicked}
});
 });
});

In your flask endpoint, you can extract the value as follows:

@app.route('/test/', methods=['GET','POST'])
def test():
     clicked=None
     if request.method == "POST":
          clicked=request.json['data']
     return render_template('test.html')
like image 91
Sugam Avatar answered Oct 11 '22 16:10

Sugam


I used the best answer but i found a bad request error. I solve this error as below:

1- remove this line from ajax request:

contentType: 'application/json;charset=UTF-8',

2- Access to data by request.form instead of request.json.

The Javascript part will similar to this:

$(document).ready(function(){
var clicked;
$(".favorite").click(function(){
clicked = $(this).attr("name");
$.ajax({
  type : 'POST',
  url : "{{url_for('test')}}",
  data : {'data':clicked}
});
 });
});

Flask part:

@app.route('/test/', methods=['GET','POST'])
def test():
      clicked=None
      if request.method == "POST":
          clicked=request.form['data']
     return render_template('test.html')
like image 33
ASSILI Taher Avatar answered Oct 11 '22 14:10

ASSILI Taher