0

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

9
  • Are you sure you don't get 403 csrf-missing answer in that ajax post? Commented Jul 5, 2012 at 10:04
  • I am quite sure I used csrf token as stated in docs.djangoproject.com/en/1.3/ref/contrib/csrf/#csrf-ajax .. anyway return code of POST is 200 Commented Jul 5, 2012 at 10:07
  • You can try printing JSON in your view just before sending the HttpResponse and verify in your dev server. Commented Jul 5, 2012 at 10:08
  • 2
    @Francesco Have you tried Firebug or Chrome developer tool to look for what data you received in response to the request? Commented Jul 5, 2012 at 10:28
  • 1
    In your JS, you want array as string or dict type? Assignment to it in for loop doesn't look what you want. Commented Jul 5, 2012 at 12:35

2 Answers 2

2

$.postJSON is about POSTing data, which is not what you need here since you're obviously not posting anything (and don't handle anythin posted in your view neither). Using $.getJSON instead is probably the first thing to do (http://api.jquery.com/jQuery.getJSON/). This would also avoid any problem with django's csrf token

Sign up to request clarification or add additional context in comments.

3 Comments

I agree with you, even if I have to post data as next step in this case. By the way, with getJSON result is the same..
the same as what? use a browser or curl/wget to query the URL - what does that give you? what should it show? put a print statement in your view to print out the returned json object - compare that to what your client is seeing. If that does not reveal the problem then edit your question showing the data returned by the view compared with the data your client says it's receiving.
Thanks @scytale .. I've found a solution. I got json via wget and printed dictionary to a file in the server, I cannot post here these files because they are massively large (48612 pairs - key/value).. However the result are: - both files contains all keys - there was a problem with javascript code.. I'll post below
0

The problem was with javascript code.. I may use this instead:

$.postJSON('ajax/routine', '', function(data) 
      {
        for(key in data)
            $("#result").html($("#result").html() + " " + key + " " + data[key]);
      });

However, this is working but it's WRONG and surely not smart, because the big amount of data to process cause the page to hang (#result innerHtml is updated 48612 times). So I'm thinking to use a webWorker to do this or make save the data instead of showing it inside the html.

Thanks to all

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.