Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

147
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!