As I have mentioned in my comment what the data object represents is a json object and unlike javascript objects, the keys must be strings see this ref
Change your data object to (am assuming your values are valid JSON data type (string, number, object, array, boolean or null))
EDIT I was answering this question and realized the main problem could be that your data was url-encoded by jquery. If you stringify it like below, it should work. Try it on your original code
data: JSON.stringify({
"question": question,
"answer1": answer1,
"answer2": answer2,
"answer3": answer3,
"answer4": answer4,
"answer5": answer5,
"answer6": answer6,
"congress": congress,
"session": session,
"date": date,
"lecture": lecture,
"correct": correct
}),
Okay, I will make your flask a bit more complicated, but for a good cause. I will make a wtf form (you can make a normal form but I like the additional features of wtf like validation etc)
class QuestionForm(FlaskForm):
question = StringField('question', [validators.InputRequired(message="Please enter a valid entry")])
answer1 = StringField('answer1', [validators.InputRequired(message="Please enter a valid entry")])
answer2 = StringField('answer2', [validators.InputRequired(message="Please enter a valid entry")])
answer3 = StringField('answer3', [validators.InputRequired(message="Please enter a valid entry")])
answer4 = StringField('answer4', [validators.InputRequired(message="Please enter a valid entry")])
answer5 = StringField('answer5', [validators.InputRequired(message="Please enter a valid entry")])
answer6 = StringField('answer6', [validators.InputRequired(message="Please enter a valid entry")])
congress = StringField('congress', [validators.InputRequired(message="Please enter a valid entry")])
session = StringField('session', [validators.InputRequired(message="Please enter a valid entry")])
date = StringField('date', [validators.InputRequired(message="Please enter a valid entry")])
lecture = StringField('lecture', [validators.InputRequired(message="Please enter a valid entry")])
correct = StringField('correct', [validators.InputRequired(message="Please enter a valid entry")])
Be sure to make relvant imports if you go with wtf
from flask_wtf import FlaskForm
from wtforms import Form, BooleanField, StringField, IntegerField, validators, SelectField, TextAreaField, HiddenField
Then I will use werkzeug.datastructures to access the json data (i start with import, import the form as well if it is in a different file from forms import QuestionForm
), then access all the values of the form. I am returning all the values in a string just to be sure I am receiving them, am sure you will know how to save to your database from there
from werkzeug.datastructures import MultiDict
@app.route('/create', methods=['POST'])
def cre():
form_data = MultiDict(mapping=request.json)
form = QuestionForm(form_data)
if request.is_json:
question= form.question.data
answer1= form.answer1.data
answer2= form.answer2.data
answer3= form.answer3.data
answer4= form.answer4.data
answer5= form.answer5.data
answer6= form.answer6.data
congress= form.congress.data
ssession= form.session.data
date= form.date.data
lecture= form.lecture.data
correct= form.correct.data
return str(question+' '+answer1+' '+answer2+' '+answer3+' '+answer4+' '+answer5+' '+answer6+' '+congress+' '+ssession+' '+date+' '+lecture+' '+correct)
You can add if form.validate_on_submit():
after the if request.is_json:
if you wish to validate the data as per the validators you specify on the form
I am able to get a response with all the values I put in my json object. I am sure if you find this example one that can work for you, you will be able to debug from there (I used ssession because on my testing app i already have session defined)