[英]error: [Errno 32] Broken pipe
我正在研究一個Django項目。 一切順利,直到我創建了一個Ajax請求,將值從html頁面發送到后端(views.py)。
當我使用Ajax發送數據時,我能夠查看傳遞給views.py的值,它甚至可以到達render_to_response方法並顯示我的頁面,但會在終端中拋出損壞的管道錯誤。 我沒有看到任何類型的程序中斷,但我想知道是否有辦法防止此錯誤發生。 我檢查了其他回復。 但到目前為止沒有運氣。
當我嘗試在刷新的頁面上再次點擊提交時,我收到以下消息:
您要查找的頁面使用了您輸入的信息。 返回該頁面可能會導致您重復執行任何操作。 你想繼續嗎? [提交] [取消]`
這是轉儲:
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日:我剛剛弄清楚了管道破裂的原因。 這是因為我沒有發回Python的響應,只是期望頁面自動刷新。 我是所有這一切的新手,並花了一些時間來弄清楚為什么會這樣。
您尚未發布任何代碼,但這可能是因為您在按鈕提交時觸發了Ajax請求但未阻止默認操作。 所以發出了Ajax請求,但是當它返回數據時,瀏覽器已經請求了下一頁,所以沒有什么可以接收它。
我通過添加以下內容解決了這個問題:
self.send_header("Access-Control-Allow-Origin", "*")
因為我在發送帖子請求頁面時發現了一些錯誤:
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is there
然后我得到了這個解決方案並解決了上述問題
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.