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