post to save answers

master
gtitov 3 years ago
parent 870e5fbcb2
commit 4d6a317882

@ -27,11 +27,12 @@ document.addEventListener("DOMContentLoaded", function () {
.then(r => r.json()) .then(r => r.json())
.then(quiz => { .then(quiz => {
// console.log(quiz) // console.log(quiz)
// console.log(quiz.questions)
var questions = quiz.questions var questions = quiz.questions
var questions_html = "<form id='form' onkeydown='return event.keyCode != 13;'>" var questions_html = "<form id='form' onkeydown='return event.keyCode != 13;'>"
questions.forEach(q => { questions.forEach(q => {
console.log(q) // console.log(q)
if (q.options) { if (q.options) {
let options_div = "" let options_div = ""
q.options.forEach(o => { q.options.forEach(o => {
@ -68,16 +69,21 @@ document.addEventListener("DOMContentLoaded", function () {
button.onclick = function () { button.onclick = function () {
const form = document.getElementById('form'); const form = document.getElementById('form');
const formData = new FormData(form); const formData = new FormData(form);
console.log(formData)
for (const [key, value] of formData) { for (const [key, value] of formData) {
console.log(quiz) // console.log(quiz)
console.log(`${key}: ${value}\n`) // assume questions are in the same order - can it make code simplier? // console.log(`${key}: ${value}\n`) // assume questions are in the same order - can it make code simplier?
quiz.questions.find(q => q.id == key).student_answer = value quiz.questions.find(q => q.id == key).student_answer = value
// console.log(quiz) // console.log(quiz)
} }
fetch('http://localhost:8000/save_student_answers?' + new URLSearchParams({ fetch('http://localhost:8000/save_student_answers', {
student_answers: JSON.stringify(quiz) method: 'POST',
})) // mode: 'no-cors',
// headers: {
// 'Accept': 'text/plain',
// 'Content-Type': 'text/plain'
// },
body: JSON.stringify(quiz)
})
document.getElementById("main").innerHTML = "<p>Тестирование окончено</p>" document.getElementById("main").innerHTML = "<p>Тестирование окончено</p>"
}; };
// where do we want to have the button to appear? // where do we want to have the button to appear?

@ -1,4 +1,4 @@
from fastapi import FastAPI from fastapi import FastAPI, Body
from fastapi.middleware.cors import CORSMiddleware # CORS from fastapi.middleware.cors import CORSMiddleware # CORS
from fastapi.staticfiles import StaticFiles from fastapi.staticfiles import StaticFiles
import json import json
@ -78,8 +78,8 @@ def get_quiz(student_id, student: str):
"questions": questions_for_student "questions": questions_for_student
} }
@app.get("/save_student_answers") @app.post("/save_student_answers")
def send_student_answers(student_answers: str): def send_student_answers(student_answers: str = Body()):
"""Save answers as-is and check answers """Save answers as-is and check answers
Args: Args:

Loading…
Cancel
Save