2

I've developed a web app with Django. It makes an AJAX call (with jQuery's $.ajax) to update an image based on certain input text. The following code is used for the purpose:

$('img#diagram').attr('src', '/images/ajax/ajax_loader.gif');
$.ajax({
    type:       'POST',
    url:        '/draw/',
    data:       { 'diagram':  $('#diagram-text').val() },
    cache:      false,
    success:    function(mesg, txtStatus, XmlHttpRequest) {
        result = $.parseJSON(mesg);
        if (result['error']) {
            alert('An error was encountered: ' + result['error']);
        }
        else {
            $('img#diagram').attr('src', result['diagram_url']);
        }
    },
    error:      function(XmlHttpRequet, txtStatus, errorThrown) {
        alert('Failed to draw the diagram!\n' + errorThrown);
    },
    dataType:   'html'
});

It works properly i.e. updates the image after inputs are changed and the 'Draw' button is clicked.

Problem is, if I don't input anything, and click on the 'Draw' button, the AJAX loader image remains there. Now, nothing happens if I do either of the following:

  • Input some text and click on the 'Draw' button
  • Reload the page

In fact, the page doesn't get reloaded.

If I now stop the Django development server, I get the following error:

Exception happened during processing of request from ('127.0.0.1', 44166)
Unhandled exception in thread started by <function inner_run at 0x9f6d5dc>
Error in sys.excepthook:
Traceback (most recent call last):
  File "/usr/lib/python2.6/dist-packages/apport_python_hook.py", line 48, in apport_excepthook
    if not enabled():
TypeError: 'NoneType' object is not callable

Original exception was:
Traceback (most recent call last):
  File "/usr/local/lib/python2.6/dist-packages/django/core/management/commands/runserver.py", line 60, in inner_run
    run(addr, int(port), handler)
  File "/usr/local/lib/python2.6/dist-packages/django/core/servers/basehttp.py", line 721, in run
    httpd.serve_forever()
  File "/usr/lib/python2.6/SocketServer.py", line 226, in serve_forever
    self._handle_request_noblock()
  File "/usr/lib/python2.6/SocketServer.py", line 283, in _handle_request_noblock
    self.handle_error(request, client_address)
  File "/usr/lib/python2.6/SocketServer.py", line 335, in handle_error
    import traceback
ImportError: No module named traceback

Can anyone suggest anything? Of course I can put a validation to prevent empty strings. But why this actually happens, since AJAX is asynchronous?

1 Answer 1

1

This happens when the client closes the connection before the server finished sending the data.

try the function done instead of success.

   $.ajax({
        url: "/draw/",
        type: "POST",
        data: { 'diagram':  $('#diagram-text').val() },
    }).done(function(data) {
        result = $.parseJSON(mesg);
        if (result['error']) {
          alert('An error was encountered: ' + result['error']);
        }
        else {
            $('img#diagram').attr('src', result['diagram_url']);
        }
    });
Sign up to request clarification or add additional context in comments.

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.