1

I have an ajax call that looks like this;

        $.ajax({
            type : 'GET',
            url: 'url',
            data: 'data='+data,
            success: function(configOut) {
                alert(configOut);
            },
            error: function(configOut) {
                alert(configOut);
            }
        });

The view that is invoked does this:

command = ['python', 'autoDeploy.py']
p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
out = p.stdout.read()
rv = p.wait()
if rv: 
    status = 400
else:
    status = 200

return HttpResponse(out, content_type="text/plain", status=status)

In the debugger I can see that I'm returning this:

(Pdb) print HttpResponse(out, content_type="text/plain", status=status)
Content-Type: text/plain

Invalid file format. Format is: "Toolname,Type,IPaddress,login,pw"

But in my success or error function I cannot seem to access that text. configOut has:

    Object {readyState: 0, setRequestHeader: function, getAllResponseHeaders: function, getResponseHeader: function, overrideMimeType: function…}

How do I get at the text that is being returned?

Something I just noticed is that in the Chrome developers tools, in the network tab for this request, in the Status/Text column, it says "(cancelled)" - not sure what that means. If I run the same request from my browser I do get a response.

I debugged this further and found that on the server side I am getting 'error: (32, 'Broken pipe')' Googling that revealed that it's because the browser has already moved on, so there is nothing to receive the response. I've tried setting the timeout to a large value, but no joy. How can I prevent that? It seems it is not possible to do a synchronous request from the browser.

Larry Martell
  • 3,526
  • 6
  • 40
  • 76
  • Check if "out" variable contains any text... And, as I know, you must provide text length when you use http responses. (I mean CONTENT_LENGTH header) – Alexander R. Apr 07 '15 at 18:39
  • Yes it does - in my example it has 'Invalid file format. Format is: "Toolname,Type,IPaddress,login,pw"' I don't see anything about providing a length in the docs (https://docs.djangoproject.com/en/1.5/ref/request-response/#django.http.HttpResponse) – Larry Martell Apr 07 '15 at 18:47
  • Ah.. so your problem is not in sending response. I think your problem is in autoDeploy.py - because you launch this file with python, and try to read the output. This means you get text "Toolname,Type,IPaddress,login,pw" form the script "autoDeploy.py". – Alexander R. Apr 07 '15 at 21:15
  • Yes, the autoDeploy.py script send that output back to the code in the view and then I want to send that back as the request response. The code in the view gets the output, and it is sending it. The problem seems to be that the browser is not waiting for it. Please see my amended post. – Larry Martell Apr 07 '15 at 21:19
  • If you write url to address bar in the browser (without using ajax, just manually write url), - what the browser gets as response? – Alexander R. Apr 07 '15 at 22:11
  • It gets the text returned by the autoDeploy.py script – Larry Martell Apr 07 '15 at 22:12
  • I solved this - the problem was that I was not returning false in the onclick handler that the ajax call was a part of. I read this here: http://stackoverflow.com/questions/7577275/jquery-ajax-requests-are-getting-cancelled-without-being-sent – Larry Martell Apr 07 '15 at 22:18

1 Answers1

0

I solved this - the problem was that I was not returning false in the onclick handler that the ajax call was a part of. I read this here:

jQuery Ajax requests are getting cancelled without being sent

Community
  • 1
  • 1
Larry Martell
  • 3,526
  • 6
  • 40
  • 76