We're creating a web application using Flask. On one html page, users answer some questions (using <form>
). During this process, they are asked to click a button on the page for a few times when they receive random popup notifications. In the .html
file, we write javascript functions to save the timestamps of users clicking on the button into an array.
We hope to store both the user's answers and the timestamps of clicking the button. Now from .html
to .py
, we pass answers using request.form
and pass the array of timestamps using ajax json (request.json
). The request.form
works very well but the request.json
always gives a None
. We have tried request.get_json()
, request.get_json(force=True)
, request.get_json(silent=True)
, but none of them works.
Our codes are like the following:
in trial.py
:
from flask import (Blueprint, flash, g, redirect, render_template, request, url_for, jsonify, json)
from werkzeug.exceptions import abort
import csv
from flaskr.db import get_db
bp = Blueprint('trial', __name__, url_prefix='/trial')
@bp.route('', methods=('GET', 'POST'))
def trial():
if request.method == 'POST':
if "Q1" in request.form:
## this part retrieves data from request.form
## skip the details in the following
db = get_db()
db.execute(
'INSERT INTO trial (...)', (...))
db.commit()
timestamps = request.json
print(timestamps) ### This is always a None
return render_template('study/trial.html')
In trial.html
:
{% extends 'base.html' %}
{% block header %}
<h1>{% block title %}Trial{% endblock %}</h1>
{% endblock %}
{% block content %}
<!-- Skip non-related details in the following -->
<button class="NotificationButton" onclick="popup()">I Got A Notification</button>
<div>
<form name="TrialQuestions" id="TrialQuestions" method="post">
<!-- skip the details of the questions and input for answers -->
</form>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="{{url_for('static', filename='jquery.js') }}">\x3C/script>')</script>
<script>
var timestamps = {"trial":[]};
function playAudio() {
<!-- skip non-related details -->
timestamps["trial"].push(remaining);
}
function popup(){
<!-- skip non-related details -->
timestamps["trial"].push(remaining);
}
function timeUp(){
document.TrialQuestions.submit();
$.ajax({
type: "POST",
url: "{{ url_for("trial.trial") }}",
contentType: "application/json",
data: JSON.stringify(timestamps),
dataType: "json",
success: function(response) {
console.log(response);
},
error: function(err) {
console.log(err);
}
});
alert("Time Is Up! Your Answers Have Been Submitted!");
}
</script>
{% endblock %}
The console.log(response);
in trial.html
works well, but the timestamps = request.json
in trial.py
always gives a None
. We have searched and tried the suggestions from the following posts but none of them solves the problem:
- Passing Javascript array in Python Flask
- Get the data received in a Flask request
- How to send information from javascript to flask route
Can anyone give some hints? Thanks a lot!