I’m trying to pass two async values from html to flask with ajax. I created a simple html with two buttons and two inputs. I want to pass each variable with a separate button click.
I am getting the following message:error message
HTML:
<form action="/" method="POST">
<input type="number" id="val1" name="val1" value="1">
<button type="submit" value="value1" name="btn1">Click1</button><br>
<input type="number" id="val2" name="val2" value="2">
<button type="submit" value="value2" name="btn2">Click2</button><br>
</form>
JS:
value1 = document.getElementById("val1").value;
value2 = document.getElementById("val2").value;
$.ajax({
type: 'POST',
url: "/",
data: JSON.stringify[(value1, value2)],
contentType: "application/json;charset=utf-8",
dataType: "json",
success: function(data){}
});
PY:
@app.route('/', methods=['GET', 'POST'])
def start():
if request.method == 'POST':
if request.form['submit'] == 'value1':
data = request.get_json()
value1 = data['value1']
print(value1)
elif request.form['submit'] == 'value2':
data = request.get_json()
value2 = data['value2']
print(value2)
return render_template('index.html')
Could someone please take a look at this and write me why this is happening, and how can I solve it?
EDIT:
I changed my .py and .js files, and I’m seeing some progress, but I still can’t pass the value to flask…
HTML:
<form action="/" method="POST">
<input type="number" id="val1" value="25" min="25" max="35">
<button type="submit" value="value1" name="btn">Click1</button><br>
<input type="number" id="val2" name="val2" value="15" min="15" max="35">
<button type="submit" value="value2" name="btn">Click2</button><br>
</form
When I click any button, it wants to print data, but I always get None
in the response
PY:
@app.route('/', methods=['GET', 'POST'])
def start():
if request.method == 'POST':
if request.form.get('btn') == 'value1':
data = request.get_json()
print(data)
elif request.form.get('btn') == 'value2':
data = request.get_json()
print(data)
return render_template('index.html')
JS:
var value1 = document.getElementById('val1').value;
var value2 = document.getElementById('val2').value;
$.ajax({
type: 'POST',
url: '/',
data: JSON.stringify({"A":value1}, {"B":value2}),
contentType: "application/json;charset=utf-8",
success: function(data){}
});
In my opinion, the problem is here part od code in .py:
data = request.get_json()
print(data)
When I changed this to print(data['A'])
then I get a lot of errors
or here in .js:
data: JSON.stringify({"A":value1}, {"B":value2}),
Could someone please help me?
2
Answers
It looks like you mistyped this line:
try replacing it with:
If you define two buttons with the type "submit" in the same form
do they behave the same. The entire form is transmitted for both.
It is easier for you to split the form if you want to transfer each value in a
separate request.
Within a transmission, the data is formatted in a predefined format.
If you submit the data as a form, you use the type "application/x-www-form-urlencoded"
or "multipart/formdata". For JSON it is "application/json".
Depending on the transmission format used, the data within your route can be
queried in different ways. In the case of a transmission as
"application/x-www-form-urlencoded" or "multipart/form-data"
they are within "request.form", with "application/json" they are within
"request.json".
The assignment of the values is based on the name of the respective input field.
Buttons are not sent.
The error message appeared because you asked for a value within a dictionary,
which was not there. When asked with
get
, the error that is thrown by the missing key is bypassed and in this caseNone
is returned.