我正在研究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请求,但是到返回数据时,浏览器无论如何已经请求了下一页,因此没有任何内容可以接收它。