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

154
Visualizações
can you get a json file from an xmlhttprequest?

I am trying to create a JSON file from the variable "request" in my python code and retrieve it in javascript with an XMLHttpRequest. My python code correctly outputs "request" to the command line, however, the console.log output of the XMLHttpRequest is 'undefined'. if I use "console.log(xmlhttp.responseText);" in the javascript portion of the code below the console outputs the entire contents of my html file. Does anyone know how to get that info from "request" over to my javascript as a JSON?

import json
import sys
import cgi

class MapDetailView(generic.DetailView):
    model = Map
    request = json.loads('{"name": "woods", "size": 1000}')
 
    def output(content):
        sys.stdout.write('Content-Type: application/json')
        sys.stdout.write(content)
 
    form = cgi.FieldStorage()
 
    fail=0
    try:
        myData = json.dumps(request)
    except:
        fail=1
    else:
        if myData == "":
            fail=1

    if fail == 1:
        output('Who are you?')
   
    output(myData)

java script code fragment

function callAjax(url)
{
    var xmlhttp;
    xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function()
    {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
        {
            console.log(xmlhttp.responseJSON);
        }
    }
    xmlhttp.open("GET", url, true);
    xmlhttp.send();
}

callAjax('http://127.0.0.1:8000/catalog/map/1');

EDIT: made the suggestions by Barmar but still having the same behavior

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