firefox request aborted

本文探讨了使用pjax时遇到的请求被频繁终止的问题。通过调整默认超时时间及避免不必要的事件绑定解决了该问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

https://github.com/defunkt/jquery-pjax/issues/46



slampis opened this issue 2 years ago

requests aborted

No milestone
No one is assigned

I am often getting requests aborted in Firefox and Google Chrome.
I tried to debug this and found this piece of code

    if ( xhr && xhr.readyState < 4) {
        xhr.onreadystatechange = $.noop
        xhr.abort()
    }

which makes perfectly sense but the requested aborted problem occurs also if there isn't another request pending.
I verified this with this code

    if ( xhr && xhr.readyState < 4) {
        alert('already pjaxing...');
    }

The popup isn't shown but the request often gets aborted. Server side everything works fine.
Any idea about this behaviour?

I guess I figured it out. It was caused by the default timeout in the PJAX requests which was to short for me.

Glad you figured this one out... been banging my head against it all morning trying to figure out whysometimes pjax was working and not others.

This is a particular issue for me with rails in development mode as the timeout is often exceeded.

You can now change the timeout globally:

$.pjax.defaults.timeout = 5000

Closed

defunkt closed the issue 2 years ago

Thank you for this! I was experiencing the same problem. Perhaps a bigger default value is in order?

Cheers,
AT

I'm also seeing many aborted calls. My app responds in < 1 second, but just to try I increased the timeout to 30000. No luck.

Often I'll see 3 aborted calls to the URL, and then a final successful call to the URL.

Any other things to look into?

I'm seeing the same thing as rdewell. Even if pjax request is under timeout, I see that JS triggers many unnecessary requests to the same URL and only one of them is finished - others are aborted before they proceed. See the screenshot below:

screenshot

Thanks, I was having this issue as well, using defaults.timeout = 5000 worked.

Just for posterity's sake -- I had been running into this problem, and was tearing my hair out.

I finally realized that I was calling the $.fn.pjax() function in my returned pjax templates with the intention of binding to newly loaded elements.

Apparently that was unnecessary, and I was instead generating multiple requests bound to the handler, and those requests returnedxhr.readyState = 1, leading to aborted requests.

This was compounded when I prevented the timeout, as the number of canceled requests continued to linearly grow and a new page load was never generated.

So for anyone that stumbles across this in the future, and extending the timeout still results in aborted requests (such as@rdewell and @dsabanin above), make sure you aren't binding extra events to your handlers.

(Note: It might be useful if the docs indicated the $.fn.pjax() binds to returned content, and calling again is unnecessary.)

Perfect. Thank you for the help, this solved my problem.


报错误如下C:\Users\70272\PyCharmMiscProject\.venv\Scripts\python.exe C:\Users\70272\PyCharmMiscProject\test.py Traceback (most recent call last): File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\urllib3\connectionpool.py", line 787, in urlopen response = self._make_request( File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\urllib3\connectionpool.py", line 488, in _make_request raise new_e File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\urllib3\connectionpool.py", line 464, in _make_request self._validate_conn(conn) File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\urllib3\connectionpool.py", line 1093, in _validate_conn conn.connect() File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\urllib3\connection.py", line 790, in connect sock_and_verified = _ssl_wrap_socket_and_match_hostname( File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\urllib3\connection.py", line 969, in _ssl_wrap_socket_and_match_hostname ssl_sock = ssl_wrap_socket( File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\urllib3\util\ssl_.py", line 480, in ssl_wrap_socket ssl_sock = _ssl_wrap_socket_impl(sock, context, tls_in_tls, server_hostname) File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\urllib3\util\ssl_.py", line 524, in _ssl_wrap_socket_impl return ssl_context.wrap_socket(sock, server_hostname=server_hostname) File "C:\Users\70272\AppData\Local\Programs\Python\Python310\lib\ssl.py", line 513, in wrap_socket return self.sslsocket_class._create( File "C:\Users\70272\AppData\Local\Programs\Python\Python310\lib\ssl.py", line 1071, in _create self.do_handshake() File "C:\Users\70272\AppData\Local\Programs\Python\Python310\lib\ssl.py", line 1342, in do_handshake self._sslobj.do_handshake() ConnectionResetError: [WinError 10054] 远程主机强迫关闭了一个现有的连接。 During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\requests\adapters.py", line 667, in send resp = conn.urlopen( File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\urllib3\connectionpool.py", line 841, in urlopen retries = retries.increment( File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\urllib3\util\retry.py", line 474, in increment raise reraise(type(error), error, _stacktrace) File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\urllib3\util\util.py", line 38, in reraise raise value.with_traceback(tb) File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\urllib3\connectionpool.py", line 787, in urlopen response = self._make_request( File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\urllib3\connectionpool.py", line 488, in _make_request raise new_e File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\urllib3\connectionpool.py", line 464, in _make_request self._validate_conn(conn) File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\urllib3\connectionpool.py", line 1093, in _validate_conn conn.connect() File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\urllib3\connection.py", line 790, in connect sock_and_verified = _ssl_wrap_socket_and_match_hostname( File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\urllib3\connection.py", line 969, in _ssl_wrap_socket_and_match_hostname ssl_sock = ssl_wrap_socket( File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\urllib3\util\ssl_.py", line 480, in ssl_wrap_socket ssl_sock = _ssl_wrap_socket_impl(sock, context, tls_in_tls, server_hostname) File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\urllib3\util\ssl_.py", line 524, in _ssl_wrap_socket_impl return ssl_context.wrap_socket(sock, server_hostname=server_hostname) File "C:\Users\70272\AppData\Local\Programs\Python\Python310\lib\ssl.py", line 513, in wrap_socket return self.sslsocket_class._create( File "C:\Users\70272\AppData\Local\Programs\Python\Python310\lib\ssl.py", line 1071, in _create self.do_handshake() File "C:\Users\70272\AppData\Local\Programs\Python\Python310\lib\ssl.py", line 1342, in do_handshake self._sslobj.do_handshake() urllib3.exceptions.ProtocolError: ('Connection aborted.', ConnectionResetError(10054, '远程主机强迫关闭了一个现有的连接。', None, 10054, None)) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\webdriver_manager\core\http.py", line 32, in get resp = requests.get( File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\requests\api.py", line 73, in get return request("get", url, params=params, **kwargs) File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\requests\api.py", line 59, in request return session.request(method=method, url=url, **kwargs) File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\requests\sessions.py", line 589, in request resp = self.send(prep, **send_kwargs) File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\requests\sessions.py", line 703, in send r = adapter.send(request, **kwargs) File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\requests\adapters.py", line 682, in send raise ConnectionError(err, request=request) requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(10054, '远程主机强迫关闭了一个现有的连接。', None, 10054, None)) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Users\70272\PyCharmMiscProject\test.py", line 11, in <module> driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install())) File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\webdriver_manager\chrome.py", line 40, in install driver_path = self._get_driver_binary_path(self.driver) File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\webdriver_manager\core\manager.py", line 40, in _get_driver_binary_path file = self._download_manager.download_file(driver.get_driver_download_url(os_type)) File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\webdriver_manager\drivers\chrome.py", line 32, in get_driver_download_url driver_version_to_download = self.get_driver_version_to_download() File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\webdriver_manager\core\driver.py", line 48, in get_driver_version_to_download return self.get_latest_release_version() File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\webdriver_manager\drivers\chrome.py", line 70, in get_latest_release_version resp = self._http_client.get(url=latest_release_url) File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\webdriver_manager\core\http.py", line 35, in get raise exceptions.ConnectionError(f"Could not reach host. Are you offline?") requests.exceptions.ConnectionError: Could not reach host. Are you offline? 进程已结束,退出代码为 1
最新发布
08-07
<think>首先,用户的问题是关于解决“BadRequestError: request aborted”的错误。这个错误在Node.js环境中常见,特别是在处理HTTP请求时,比如使用Express框架。 从系统指令中,我需要: - 使用$...$格式表示行内数学表达式(但这里可能不涉及数学)。 - 使用$$...$$格式表示独立公式(同样,可能不适用)。 - 确保LaTeX语法正确。 - 使用中文回答。 - 生成相关问题。 - 在回答中引用的段落末尾添加引用标识,如[^1]。 - 回答结构清晰,逐步解决问题。 - 参考提供的引用。 提供的引用: - [^1]: 描述了一个问题,其中添加新路由后POST请求导致错误,可能与body-parser和express-formidable冲突。 - [^2]: 类似问题,确认body-parser和express-formidable冲突。 - [^3]: 另一个例子,在HTTP请求中content-length设置不正确导致错误。 用户的需求是解决“BadRequestError: request aborted”错误。常见原因包括: 1. 中间件冲突(如body-parser和express-formidable)。 2. 请求头设置不正确(如Content-Length)。 3. 请求超时或客户端中止请求。 4. 其他配置问题。 我的回答应该结构清晰: - 先解释错误原因。 - 然后提供逐步解决方法。 - 最后生成相关问题。 步骤: 1. **引言**:简要介绍错误。 2. **常见原因**:列出可能的原因。 3. **解决方法**:逐步指导如何解决。 4. **总结**:强调关键点。 5. **相关问题**:生成相关问题。 使用中文回答。 生成相关问题:基于主题,如调试技巧、替代方案等。 引用添加:在引用来源时,在末尾添加[^1], [^2], [^3]。 最终回答结构: - 开头:解释错误。 - 主体:原因和解决步骤。 - 结尾:总结和相关问题。</think>### BadRequestError: request aborted 解决方法 "BadRequestError: request aborted" 错误通常在 Node.js 环境中发生,尤其是在处理 HTTP 请求(如使用 Express 框架)时。这个错误表明服务器在处理请求时被中止,可能由于客户端请求中断、中间件冲突或请求头配置错误。下面我将一步步解释原因并提供可靠的解决方法。请确保您的项目环境(如 Node.js 版本、依赖包)是最新的,以避免兼容性问题。 #### 常见原因分析 1. **中间件冲突**:多个 body 解析中间件(如 `body-parser` 和 `express-formidable`)同时使用时,会导致请求处理冲突,从而中止请求[^1][^2]。 2. **请求头设置错误**:`Content-Length` 头与实际发送的数据长度不匹配,导致服务器拒绝请求[^3]。 3. **请求超时或客户端中止**:客户端在网络问题下提前关闭连接,或服务器响应时间过长。 4. **其他配置问题**:如路由处理不当、请求体过大或异步操作错误。 #### 逐步解决方法 请根据您的具体场景选择以下步骤。建议从最简单的检查开始: 1. **检查并修复中间件冲突**: - 如果您的项目中同时使用了 `body-parser` 和类似 `express-formidable` 的包,移除其中一个以避免冲突。 - 示例:在 Express 应用中,只保留一个 body 解析器。修改 `app.js` 文件: ```javascript const express = require('express'); const bodyParser = require('body-parser'); // 移除 express-formidable 或其他冲突包 const app = express(); // 只使用 body-parser app.use(bodyParser.json()); // 用于 JSON 请求 app.use(bodyParser.urlencoded({ extended: true })); // 用于 URL-encoded 请求 ``` - 测试:重新运行应用,发送 POST 请求验证是否解决。如果错误消失,说明是中间件冲突导致的[^1][^2]。 - 如果必须使用多个解析器,尝试调整中间件顺序或使用兼容方案(如 `express.json()` 替代)。 2. **校正请求头设置**: - 检查客户端代码中的 `Content-Length` 头是否与请求体数据长度一致。不一致时,服务器会中止请求。 - 示例:在 Node.js HTTP 请求中,确保 `Content-Length` 使用 `Buffer.byteLength()` 计算: ```javascript const http = require('http'); const postData = JSON.stringify({ key: 'value' }); // 请求体数据 const options = { hostname: 'localhost', port: 3000, path: '/your-route', method: 'POST', headers: { 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(postData) // 正确计算长度 } }; const req = http.request(options, (res) => { // 处理响应 }); req.write(postData); req.end(); ``` - 测试:运行请求代码,如果错误解决,说明是头部配置问题[^3]。 - 如果不需要精确控制,可以省略 `Content-Length` 头(Node.js 会自动计算),但不推荐用于生产环境。 3. **处理请求超时或客户端问题**: - 增加服务器超时设置:在 Express 中,添加超时中间件: ```javascript app.use((req, res, next) => { req.setTimeout(5000); // 设置 5 秒超时 next(); }); ``` - 检查客户端网络:确保客户端(如浏览器或 Postman)没有手动中止请求。使用开发者工具监控网络请求状态。 - 优化大型请求:如果请求体过大(如文件上传),使用流式处理或分块传输。 4. **通用调试技巧**: - **日志记录**:添加详细日志来定位错误点: ```javascript app.use((err, req, res, next) => { console.error('Error:', err.stack); // 捕获并打印错误堆栈 res.status(500).send('Server error'); }); ``` - **更新依赖**:运行 `npm update` 确保所有包(如 Express、body-parser)是最新版本。 - **简化代码**:临时移除新添加的路由或功能,逐步测试以隔离问题。 #### 总结 "BadRequestError: request aborted" 通常由中间件冲突或请求头错误引起。优先检查: - 移除冲突的中间件(如只保留 `body-parser`)。 - 确保 `Content-Length` 头正确。 - 如果问题持续,提供更多代码片段(如您的路由和处理函数)以便进一步分析。大多数情况下,上述步骤能快速解决错误[^1][^2][^3]。 如果您已尝试这些方法但问题未解决,请分享您的代码和错误日志,我可以帮助深入诊断。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值