skip to Main Content

I need to pass values from my JS code to my Flask code. I am creating a JSON in my JS code and I used AJAX to send it via POST method to my Flask :

$("#btnSubmitRejetRefModele").click(function() {
    var movies = {
        'title': 'SPY',
        'release_date': '12/02'
    }   

    $.ajax({
        url: '/rejets_modeles',
        type: 'POST',
        datax: JSON.stringify(movies),
        dataType: "json"

    }).done(function(result){
        //console.log(result)
    })
}); 

And my python code app.py :

@app.route('/rejets_modeles', methods=["POST","GET"])
def rejets_modeles():
   if request.method == 'POST':
    datax = {}
    datax = request.get_json()

    print("--------------")
    print(datax)

And the result of this in my Terminal :

————– None

How can I get my datax printed in my python code ? Thank you

2

Answers


  1. JS sent it as form data so try print(request.form)

    Login or Signup to reply.
  2. ImmutableMultiDict([(‘{“title”:”SPY”,”release_date”:”12/02″}’, ”)]), now how can I store just the json into a variable ?

    When an ajax call comes to flask, itll already be a proper python dict thus no need for any extra stuff, thus should be able to use title = request.form['title'] for example.

    $.ajax({
        url: '/rejets_modeles',
        type: 'POST',
        datax: movies,
        dataType: "json"
    
    })
    

    without stringify, there’s no need to make it into a string and it’ll work as expected (probably).

    Incoming request data (flask docs), Get the data received in a Flask request

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search