I am trying to return a json response for an ajax request with django. Json response is a python dictionary serialized. I am sure that dictionary contains enough data, but it doesn't arrive to client side. Server side I have this python routine:
def routine(request):
response_dict = {}
f = open("output.txt", "r")
for line in f:
line.strip('\n ')
(key, val) = line.split('\t')
if re.search("^[a-zA-Z][a-zA-Z0-9]*$", key) != None:
if re.search("^[0-9]+$", val) != None:
response_dict[key] = val
f.close()
json_response = json.dumps(response_dict)
return HttpResponse(json_response, mimetype='application/json')
Client side I have this javascript+jQuery routine (EDITED below, see that version):
$.postJSON('ajax/routine', '', function(data)
{
console.debug(data);
console.debug(data.result);
$("#result").html(data.result);
});
postJSON is a jQuery plugin that does a POST ajax request, the code is the following:
$.postJSON = function(url, data, callback) {
return jQuery.ajax({
'type': 'POST',
'url': url,
'contentType': 'application/json',
'data': JSON.stringify(data),
'dataType': 'json',
'success': callback
});
};
No output is written neither in console nor in tag identified with "result".
Edit now it is returning 200 Http status code, and 124 as data.result . In the server I debugged and json.dumps works flawlessly.
Edit2 I've edited my jQuery/javascript code:
$.postJSON('ajax/routine', '', function(data)
{
array = {};
for(key in data)
{
array = key + " " + data[key];
}
$("#result").html(array);
});
I've tested it under Firebug and response arrives to client but: 1) with small array (1 element) it is printed out 2) response makes me crash both Chromium developer tools and Firebug (when opening JSON tab, in response tab I get only a piece of response and the message "Firebug response size limit has been reached. Click here to open the entire response in a new Firefox tab".
Thanks