I'm trying to make an app for my University. I have to get the student attendance and store the name, surname, student_id, date, time and localization in an sql table. I tried some code lines but always take the same warning 'Did not attempt to load JSON data because the request Content-Type was not 'application/json'.'
My objective is that when clicking in the button 'Pasar presente' in the webpage 'mis_cursos', the system automatically redirect me to the 'student_attending' route and use the previous information to set the values into the table.
I'm using html+css in the front end code:
<script type="text/javascript">
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
alert("Geolocation is not supported by this browser.");
}
}
function showPosition(position) {
document.getElementById('latitude').value = position.coords.latitude;
document.getElementById('longitude').value = position.coords.longitude;
}
document.getElementById('attendanceForm').addEventListener('submit', function(e) {
e.preventDefault();
const formData = new FormData(this);
fetch('/submit', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => alert(data.message))
.catch(error => console.error('Error:', error));
});
</script>
And Flask in the backend:
@app.route('/student-attending', methods=['GET'])
def pasar_presente():
ubicacion_req = request.json
geolocator = Nominatim(user_agent="GetLoc")
location = geolocator.geocode(ubicacion_req)
ubicacion_actual = (location.latitude, location.longitude)
Legajo = request.args.get('Legajo')
Email = request.args.get('Email')
ID_Curso = request.args.get('ID_Curso')
fecha_actual = datetime.now().strftime('%Y-%m-%d')
hora_actual = datetime.now().strftime('%H:%M:%S')
cursor=mysql.connection.cursor()
cursor.execute("INSERT INTO `asistencia` (`ID_Curso`, `Legajo`, `Email`, `Fecha`, `Hora`, `Ubicación`) VALUES (%s, %s, %s, %s, %s, %s)",(ID_Curso, Legajo, Email, fecha_actual, hora_actual, ubicacion_actual))
mysql.connection.commit()
return redirect(url_for('mis_cursos', asistencia_ok='asistencia registrada correctamente'))