I'm completely new to Ajax requests. I'm trying to send data from a webpage to my Flask backend using an Ajax request, but I can't get anything to show up in the backend:
Here is the request code I am using:
function confirm() {
const xhttp = new XMLHttpRequest();
const data = document.getElementById("tableID");
xhttp.open("POST", "app.py");
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send(data);
console.log(xhttp);
console.log(data);
}
When viewing in the Google Chrome console, the request and data appear correctly like this:
<table id="tableID">
<tbody>
<tr>...</tr>
<tr>...</tr>
<tr>...</tr>
</tbody>
</table>
This is how my backend looks:
@app.route('/admintools', methods=["POST", "GET"])
def admintools():
tracks = observed_tracks(get_tracks())
if request.method == "POST":
print("request.method == POST")
print(request.form)
if request.method == "GET":
print("request.method == GET")
print(request.form)
return render_template("tools/admintools.html", tracks=tracks)
However, nothing appears in the terminal except for:
request.method == GET
ImmutableMultiDict([])
(I never mention "GET" request in the HTML page at all) Any ideas on what the issue might be?