Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

142
Visualizações
Send XMLHttpRequest POST to python cgi

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 -
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda