I am sending a text string from javascript to a cgi python3 program. User initiating the send with a button.
With the simplified code I made below the server seems to acknowledge the POST but the cgi does not run. (no output is shown). However if I refresh the page where my button is then the output of the cgi program is now shown on stderr. Strange behaviour.
Any pointers?
I am using a python cgi server.
python3 -m http.server --cgi 8000
the html file:
<html>
<head>
<script>
function post_data(){
var xhr = new XMLHttpRequest();
xhr.open("POST", "/cgi-bin/cgi.py");
xhr.setRequestHeader('Content-Type', 'text/html');
xhr.send("hello world");
xhr.onload = function() {
console.log(`Loaded: ${xhr.status} ${xhr.response}`);
};
xhr.onerror = function() {
console.log(`Network Error`);
};
}
</script>
<body>
<button type="button" class="btn btn-primary" onclick="post_data()">Send</button>
</body></html>
The python cgi:
#!/usr/bin/python3
import sys
sys.stderr.write(sys.stdin.read())
print("Content-Type: text/html\n\n")
print("<html><body>ok</body></html>")
The output once the button is clicked:
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...
127.0.0.1 - - [15/Oct/2021 17:03:38] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [15/Oct/2021 17:03:39] "POST /cgi-bin/cgi.py HTTP/1.1" 200 -
after refreshing the page this additional output comes:
hello world127.0.0.1 - - [15/Oct/2021 17:04:03] "GET / HTTP/1.1" 304 -