I made a web page in HTML, CSS and JS where there is a form that must store data in a SQLite database and I have used Python (with Flask) as the intermediary API between the form and the database. The problem is that when I register the data by clicking on the register button, in the console (because I am using Python's localhost) I get this message:code 501, message Unsupported method ('POST')"POST /subpaginas/database/machine_reg HTTP/1.1" 501 -
How can I solve this problem? Here my form codes, in javascript and the Python code
Form html code:
<div class = "reg_form"><form id = "registrationForm" onsubmit="submitForm(); return false;"><div class = "row"><div class="col-md-4"><label for="validationDefault01" class="form-label">No. serie</label><input type="text" class="form-control" id="validationDefault01" required></div><div class="col-md-4"><label for="validationDefaultUsername" class="form-label">Ubicación</label><div class="input-group"><span class="input-group-text" id="inputGroupPrepend2">📍</span><input type="text" class="form-control" id="validationDefaultUsername" aria-describedby="inputGroupPrepend2" required></div></div><div class="col-md-4"><label for="validationDefault04" class="form-label">Estado</label><select class="form-select" id="validationDefault04" required><option selected disabled value="">Elige...</option><option>Encendido</option><option>Apagado</option></select></div><p id="registrationMessage" style="display: none;">Se han registrado los datos.</p><div class="col-12"><button class="btn btn-primary" type="submit">Registrar</button></div></div></form>
JavaScript code:
function submitForm(){ var form = document.getElementById('registrationForm'); var formData = new FormData(form); var xhr = new XMLHttpRequest(); xhr.open("POST", "http://localhost:8000/subpaginas/database/machine_reg"); xhr.setRequestHeader("Content-Type", "application/json"); xhr.onreadystatechange = function() { if (xhr.readyState == 4) { if (xhr.status == 200) { var registrationMessage = document.getElementById('registrationMessage'); registrationMessage.style.display = 'block'; setTimeout(function() { registrationMessage.style.display = 'none'; }, 3000); } else { console.error(xhr.statusText); } } }; xhr.send(formData); /* var jsonData = {}; formData.forEach(function (value, key) { jsonData[key] = value; }); xhr.send(JSON.stringify(jsonData));*/}
Python code:
import sqlite3from flask import Flask, request, jsonifyapp = Flask(__name__)@app.route('/subpaginas/database/machine_reg', methods = ['POST'])def registrar_maquina(): #data = request.json #data.get serial_no = request.form.get('validationDefault01') status = request.form.get('validationDefault04') location = request.form.get('validationDefaultUsername') conn = sqlite3.connect('baseuno.sqlite') cursor = conn.cursor() cursor.execute(f"INSERT INTO machines (serial_no,status,location) VALUES ({serial_no}, {status}, {location})") conn.commit() conn.close() return jsonify({"message" : "Maquina registrada exitosamente"})if __name__ == '__main__': app.run(debug=True , port=8000)
Note: The HTML code where the form is located and the JS are located in the same folder, which is a subfolder since the forum is located on a subpage of my main page, the Python code and the database are located in a subfolder that is in the same folder as the form HTML
I tried using this block of code (found as a comment in the JavaScript code):
var jsonData = {}; formData.forEach(function (value, key) { jsonData[key] = value; }); xhr.send(JSON.stringify(jsonData));
And it didn't work either