简体   繁体   English

在长时间运行的AJAX调用中离开页面时,防止Django异常

[英]Prevent Django exception when navigating away from page during long running AJAX call

In my application, I fire off an Ajax get request using Jquery on page load. 在我的应用程序中,我在页面加载时使用Jquery触发了Ajax get请求。 If the call takes an unusually long time, the user may navigate away from the initial page during their standard browsing session. 如果呼叫花费了异常长的时间,则用户可以在其标准浏览会话期间导航离开初始页面。 Eventually, this ajax call completes and fires a response back. 最终,此ajax调用完成并发出响应。 The problem is, by this time the user has left the page and nothing is listening for a response any more. 问题在于,此时用户已经离开页面,没有人再听任何响应了。 I get this exception in my logs when that occurs. 发生这种情况时,我会在日志中收到此异常。

----------------------------------------
Exception happened during processing of request from ('98.x.x.x', 36495)
Traceback (most recent call last):
  File "M:\Python2.7\App\lib\SocketServer.py", line 582, in process_request_thread
    self.finish_request(request, client_address)
  File "M:\Python2.7\App\lib\SocketServer.py", line 323, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "M:\Python2.7\App\lib\site-packages\django\core\servers\basehttp.py", line 139, in __init__
    super(WSGIRequestHandler, self).__init__(*args, **kwargs)
  File "M:\Python2.7\App\lib\SocketServer.py", line 640, in __init__
    self.finish()
  File "M:\Python2.7\App\lib\SocketServer.py", line 693, in finish
    self.wfile.flush()
  File "M:\Python2.7\App\lib\socket.py", line 303, in flush
    self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 10053] An established connection was aborted by the software in your host machine
----------------------------------------

How can I prevent this from happening? 如何防止这种情况发生?

None of the files listed in the exception are my code. 例外中列出的所有文件都不是我的代码。

这个问题与在请求新页面且答案相同时如何在Django中取消ajax请求的功能相同。

This will only happen when you use the django development server, which tries to log exceptions on the console. 仅当您使用django开发服务器尝试在控制台上记录异常时,才会发生这种情况。

However, with other webserver (like apache) you will get similar exception but probably not so much descriptive. 但是,使用其他Web服务器(例如apache),您将获得类似的异常,但可能描述性不强。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM