最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

python - Send variable from JavaScript into Flask - Stack Overflow

matteradmin5PV0评论

I'm a plete beginner here and facing a problem in passing javascript variable into flask. Please note that my question is pletely different from previous question that has been asked.

This is my code:

JavaScript

var dd = {'mvar': 1};

    $.ajax({
      type: "POST",
      contentType: "application/json; charset=utf-8",
      url: "/",
      data: JSON.stringify(dd),
      success: function (data) {
        alert("DONE!")
      },
      dataType: "json"
    });

Flask(edited)

@app.route('/', methods=['GET', 'POST'])
def new():

  form = InputForm(request.form)
  v = request.get_json().get('mvar')
  print(v)

  return render_template('new.html',form=form,v=v)

However, when the output that I get when print out the result is "None" while I expected it to be "1"

Really hope that experts can help me, thank you!

I'm a plete beginner here and facing a problem in passing javascript variable into flask. Please note that my question is pletely different from previous question that has been asked.

This is my code:

JavaScript

var dd = {'mvar': 1};

    $.ajax({
      type: "POST",
      contentType: "application/json; charset=utf-8",
      url: "/",
      data: JSON.stringify(dd),
      success: function (data) {
        alert("DONE!")
      },
      dataType: "json"
    });

Flask(edited)

@app.route('/', methods=['GET', 'POST'])
def new():

  form = InputForm(request.form)
  v = request.get_json().get('mvar')
  print(v)

  return render_template('new.html',form=form,v=v)

However, when the output that I get when print out the result is "None" while I expected it to be "1"

Really hope that experts can help me, thank you!

Share Improve this question edited Aug 3, 2017 at 5:15 iyzad asked Aug 3, 2017 at 1:43 iyzadiyzad 812 gold badges2 silver badges10 bronze badges 4
  • You can using javascript set value of mvar to session and get value of session in flask – tuannv256 Commented Aug 3, 2017 at 2:01
  • Can you teach me how to do it? I did not learn on session yet – iyzad Commented Aug 3, 2017 at 2:04
  • I'm not good at JavaScript. Do you know how to set session variable like this. To get session variable in Flask, refer Flask session – tuannv256 Commented Aug 3, 2017 at 2:58
  • I'm not sure how it gonna be – iyzad Commented Aug 3, 2017 at 3:00
Add a ment  | 

2 Answers 2

Reset to default 2

The Request.get_json() method is what you are looking for in your Flask code.

data = request.get_json()

edit here is the exact pattern i use which works:

javascript:

$.ajax({
    type: "POST",
    url: "{{ url_for("get_post_json") }}",
    contentType: "application/json",
    data: JSON.stringify({hello: "world"}),
    dataType: "json",
    success: function(response) {
        console.log(response);
    },
    error: function(err) {
        console.log(err);
    }
});

python:

@app.route('/_get_post_json/', methods=['POST'])
def get_post_json():    
    data = request.get_json()

    return jsonify(status="success", data=data)

Instead of using

v = request.form.get('mvar', type=int)

Use this

v = request.get_json().get('mvar')

Once you print v then you should get it.

Post a comment

comment list (0)

  1. No comments so far