Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send data to Flask via AJAX?

Tags:

python

ajax

flask

I'm making small web project based on Flask. And I have to send some data to Flask, but I don't know how to do it. I tried different ways, tried to use JSON, but I don't know how to work with it. Maybe someone can share working piece of code with me or help by explaining what I have to do?

new_freq = $('#input').val() //value I want to send 
$.ajax({ 
    url: '/set_freq', 
    type: 'POST', 
    data: ,
    success: function(response){ 
        $('#main').text(response) 
    } 
})
like image 490
Bohdan Avatar asked Dec 25 '16 18:12

Bohdan


People also ask

How do you transfer data to a flask?

Flask sends form data to template Flask to send form data to the template we have seen that http method can be specified in the URL rule. Form data received by the trigger function can be collected in the form of a dictionary object and forwarded to the template to render it on the corresponding web page.

Can we use Ajax in flask?

We can also use Ajax to handle the user input rather than rendering a template. After carrying out the calculation client-side we will then pass the user input and results to the server to store in a database.

Can Ajax send data?

ajax() method allows you to send asynchronous http requests to submit or retrieve data from the server without reloading the whole page. $. ajax() can be used to send http GET, POST, PUT, DELETE etc.


1 Answers

new_freq = $('#input').val() //value I want to send 
$.ajax({ 
    url: '/set_freq', 
    type: 'POST', 
    data: new_freq,
    success: function(response){ 
        $('#main').text(response) 
    } 
})

in Flask

new_freq = request.get_data()
like image 98
Simon J. Liu Avatar answered Oct 02 '22 00:10

Simon J. Liu