Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

161
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda