我正在研究一个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请求,但是当它返回数据时,浏览器已经请求了下一页,所以没有什么可以接收它.