0

I got this error each time taht I make an ajax request, only in this project. The codes involved are listed here:

[JS AJAX CALL]

    var url = '/eden/find_flights/';
    var csrf_token = getCookie('csrftoken');

    $.ajax({
        'url': url,
        'data':  {
            'trip': JSON.stringify(trip),
            'travellers': JSON.stringify(travellers),
            'csrfmiddlewaretoken': csrf_token
        },
        'type': 'POST',
        'dataType': 'json',
        'complete': function(response) {

            $("#result").html(response.responseText); //.animate({'marginTop': "-=700px"}, 'slow', 'swing', function() {});
        },
    });

[views.py]

@csrf_exempt
def find_flights(request):

    flights = {}

    return render_to_response('flight_search_results.html', flights, context_instance=RequestContext(request))

The dict flights will be filled with data, but isn't important now. The problm that I got is that obtain a red line (like error) in firebug, but no error signal (if I use the "Open in a New Tab" option I got a blank page) and in the django console:

[05/Aug/2014 15:29:38] "POST /eden/find_flights/ HTTP/1.1" 200 2
Traceback (most recent call last):
File "/usr/lib/python2.7/wsgiref/handlers.py", line 86, in run self.finish_response()
File "/usr/lib/python2.7/wsgiref/handlers.py", line 128, in finish_response self.write(data)
File "/usr/lib/python2.7/wsgiref/handlers.py", line 212, in write self.send_headers()
File "/usr/lib/python2.7/wsgiref/handlers.py", line 270, in send_headers self.send_preamble()
File "/usr/lib/python2.7/wsgiref/handlers.py", line 194, in send_preamble 'Date: %s\r\n' % format_date_time(time.time())
File "/usr/lib/python2.7/socket.py", line 324, in write self.flush()
File "/usr/lib/python2.7/socket.py", line 303, in flush self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 32] Broken pipe
[05/Aug/2014 15:29:38] "POST /eden/find_flights/ HTTP/1.1" 500 59
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 46750)
Traceback (most recent call last):
File "/usr/lib/python2.7/SocketServer.py", line 593, in process_request_thread self.finish_request(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 334, in finish_request self.RequestHandlerClass(request, client_address, self)
File "/usr/local/lib/python2.7/dist-packages/django/core/servers/basehttp.py", line 126, in __init__ super(WSGIRequestHandler, self).__init__(*args, **kwargs)
File "/usr/lib/python2.7/SocketServer.py", line 651, in __init__ self.finish()
File "/usr/lib/python2.7/SocketServer.py", line 710, in finish self.wfile.close()
File "/usr/lib/python2.7/socket.py", line 279, in close self.flush()
File "/usr/lib/python2.7/socket.py", line 303, in flush self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 32] Broken pipe
----------------------------------------

I've looked for this error, but I couldn't understand wtf cause it, nor whatt I can do to avoid it.

petkostas
  • 7,250
  • 3
  • 26
  • 29
user2540457
  • 1
  • 1
  • 1

1 Answers1

2

You have this code:

var url = '/eden/find_flights/';
var csrf_token = getCookie('csrftoken');

$.ajax({
    'url': url,
    'data':  {
        'trip': JSON.stringify(trip),
        'travellers': JSON.stringify(travellers),
        'csrfmiddlewaretoken': csrf_token
    },
    'type': 'POST',
    'dataType': 'json',
    'complete': function(response) {

        $("#result").html(response.responseText); //.animate({'marginTop': "-=700px"}, 'slow', 'swing', function() {});
    },
});

Does this belong to a .submit() event handler? If it does, have your event call preventDefault. Otherwise your page would be loading and stopping the ajax call.

Luis Masuelli
  • 12,079
  • 10
  • 49
  • 87
  • Why does this only have 1 thumb up? I encountered this issue in React and it did the trick. More info https://reactjs.org/docs/handling-events.html – ratsimihah Mar 28 '18 at 08:03