错误: [Errno 32] 管道破裂
我正在做一个Django项目。一切都很顺利,直到我创建了一个Ajax请求,用来把值从HTML页面发送到后台(views.py)。
当我通过Ajax发送数据时,我能看到这些值被传递到views.py,并且它甚至能到达render_to_response方法,显示我的页面,但在终端却出现了“broken pipe”的错误。我没有看到程序有什么中断,但我想知道有没有办法防止这个错误出现。我查看了其他的回答,但到现在为止还没有找到解决办法。
当我在刷新后的页面上再次点击提交时,我收到了这个消息:
您正在查看的页面使用了您输入的信息。返回该页面可能会导致您采取的任何操作被重复。您想继续吗?[提交] [取消]
这是我的错误信息:
Traceback (most recent call last):
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 34812)
----------------------------------------
File "/usr/lib/python2.7/dist-packages/django/core/servers/basehttp.py", line 284, in run
self.finish_response()
File "/usr/lib/python2.7/dist-packages/django/core/servers/basehttp.py", line 324, in finish_response
self.write(data)
File "/usr/lib/python2.7/dist-packages/django/core/servers/basehttp.py", line 403, in write
self.send_headers()
File "/usr/lib/python2.7/dist-packages/django/core/servers/basehttp.py", line 467, in send_headers
self.send_preamble()
File "/usr/lib/python2.7/dist-packages/django/core/servers/basehttp.py", line 385, in send_preamble
'Date: %s\r\n' % http_date()
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
Traceback (most recent call last):
File "/usr/lib/python2.7/SocketServer.py", line 284, in _handle_request_noblock
self.process_request(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 310, in process_request
self.finish_request(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 323, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/usr/lib/python2.7/dist-packages/django/core/servers/basehttp.py", line 570, in __init__
BaseHTTPRequestHandler.__init__(self, *args, **kwargs)
File "/usr/lib/python2.7/SocketServer.py", line 640, in __init__
self.finish()
File "/usr/lib/python2.7/SocketServer.py", line 693, in finish
self.wfile.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
更新: 这是我发送的代码:
$( document ).ready(function() {
$.csrftoken();
$("#submitdata").click(function(){
//values = [tmode, fmode, t_cool, t_heat, hold];
values = {
"tmode": tmode,
"fmode": fmode,
"t_cool": t_cool,
"t_heat": t_heat,
"hold": hold
};
var jsonText = JSON.stringify(values);
$.ajax({
url: "/submitdata/",
type: 'POST',
data: jsonText,
dataType: 'json',
success:function(data){
console.log(data.success);
},
complete:function(){
console.log('complete');
},
error:function (xhr, textStatus, thrownError){
console.log(thrownError);
console.log(obj);
}
});
});
});
这是我的views.py:
@login_required
def submitvalues(request):
#context = RequestContext(request)
if request.POST:
jsonvalues = json.loads(request.raw_post_data)
print jsonvalues
return HttpResponse(json.dumps(dict(status='updated')), mimetype="application/json")
我仍然面临同样的问题。有人能帮我解决这个吗?
2014年5月28日更新: 我刚刚找到了“broken pipe”的原因。是因为我没有从Python发送回响应,只是期待页面自动刷新。我对这一切还是个新手,花了一些时间才弄明白为什么会发生这种情况。
2 个回答
0
我通过添加以下内容解决了这个问题:
self.send_header("Access-Control-Allow-Origin", "*")
因为我在发送POST请求的页面上发现了一些错误:
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is there
然后我找到了这个解决方案,解决了之前的问题。
22
你没有发任何代码,但这可能是因为你在点击按钮提交时触发了Ajax请求,但没有阻止默认的行为。所以Ajax请求是发出了,但等到数据返回时,浏览器已经请求了下一页,这样就没有地方来接收这些数据了。