每次我发出ajax请求时都会收到此错误,仅在此项目中。 所涉及的代码列于此处:
[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))
dict航班将填充数据,但现在并不重要。我得到的问题是在firebug中获得一条红线(如错误),但没有错误信号(如果我使用"在新标签中打开"选项我有一个空白页面)和django控制台:
[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
----------------------------------------
我已经找到了这个错误,但我无法理解wtf导致它,也无法避免它。
答案 0 :(得分:2)
您有以下代码:
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() {});
},
});
这属于.submit()
事件处理程序吗?如果是,请进行event
来电preventDefault
。否则,您的页面将加载并停止ajax调用。