Ajax 调用被浏览器取消

发布于 2024-12-05 13:39:37 字数 611 浏览 3 评论 0原文

我正在使用 Prototype JS 框架进行 Ajax 调用。这是我的代码:

new Ajax.Request( '/myurl.php', {method: 'post', postBody: 'id='+id+'&v='+foo, onSuccess: success, onFailure: failed} );

function success(ret) {
console.log("success",ret.readyState, ret.status);
}
function failed(ret) {
console.log("failed",ret.readyState, ret.status);
}

大多数时候,这工作正常,并且成功函数被调用,状态代码为 200。在 Safari 上,大约 5% 的时间,成功函数被调用,状态代码为 0。在这种情况下,当我查看 Web 检查器的“网络”选项卡时,会列出 ajax 调用,状态为“已取消”。我可以通过服务器日志确认该请求从未到达服务器。就好像 ajax 请求立即被取消,甚至没有尝试连接到服务器。我还没有找到任何可靠的方法来重现这个,它似乎是随机的。我做了20次,结果只有一次。

有谁知道什么会导致 ajax 调用被取消或返回状态代码 0?

I am using the Prototype JS framework to do Ajax calls. Here is my code:

new Ajax.Request( '/myurl.php', {method: 'post', postBody: 'id='+id+'&v='+foo, onSuccess: success, onFailure: failed} );

function success(ret) {
console.log("success",ret.readyState, ret.status);
}
function failed(ret) {
console.log("failed",ret.readyState, ret.status);
}

Most of the time, this works fine and the success function is called with a status code of 200. About 5% of the time on Safari the success function is called with a status code of 0. In this case, when I look in the Network tab of the web inspector, the ajax call is listed with a status of "canceled". I can confirm with server logs, that the request never hit the server. It's as if the ajax request was immediately canceled without even trying to connect to the server. I have not found any reliable way to reproduce this, it seems to be random. I do it 20 times and it happens once.

Does anyone know what would cause the ajax call to get canceled or return a status code of 0?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晨光如昨 2024-12-12 13:39:37

原因可能是您使用的http服务器和浏览器的组合。这看起来不像是 PrototypeJS 库的错误。

多个消息来源指出,HTTP 连接的 keep-alive 参数似乎在 Safari 中被破坏(请参阅此处此处此处)。在 Apache 上,他们建议将其添加到配置中:(

BrowserMatch "Safari" nokeepalive

请检查服务器文档中的适当语法)。

如果 Safari 无法很好地处理与服务器的 HTTP 持久连接,这可能可以解释您所经历的情况。

如果它对你来说不太复杂,我会尝试另一个 HTTP 服务器,每个操作系统都有很多可用的服务器。

不过,我们缺乏一些信息来完整回答您的答案。服务器问题是一个原因,但可能还有其他问题。很高兴知道它是否在其他浏览器中执行相同的操作(带有 Firebug 的 Firefox 将显示此类信息,Chrome、Opera 和 IE 有开发内置工具箱)。另一个有效的问题是每秒执行此 AJAX 请求的频率(如果相关)。

The cause may be the combination of http server and browser you are using. It doesn't seems like an error of the PrototypeJS library.

Multiple sources states that keep-alive parameter of the HTTP connection seems to be broken in Safari (see here, here or here). On Apache, they recommend adding this to the configuration:

BrowserMatch "Safari" nokeepalive

(Please check the appropriate syntax in your server documentation).

If Safari handles badly HTTP persistent connections with your server, it may explain what you experiences.

If it's not too complex for you, I would try another HTTP server, there are plenty available on every OS.

We lack a bit of information to answer fully your answer, though. The server issue is a lead but there may be others. It would be nice to know if it does the same thing in other browsers (Firefox with Firebug will display this kind of information, Chrome, Opera and IE have development builtin toolboxes). Another valid question would be how often you execute this AJAX request per second (if relevant).

秋凉 2024-12-12 13:39:37

我知道这是一个老话题,但我想分享一个 Safari 的解决方案,可能会节省其他人一些时间。下面这行确实解决了所有问题:

BrowserMatch "^(?=.*Safari)(?=.*Macintosh)(?!.*Chrom).*" nokeepalive gzip-only-text/html

正则表达式确保仅检测到 Mac 上的 Safari,而不检测到移动 Safari 和 Chrome(ium) 等。 Windows 版 Safari 也不匹配,但 keepalive 问题似乎仅是 Mac-Safari 组合的问题。此外,某些 Safari 版本不能很好地处理 gzipped css/js。

网站崩溃或 CSS 在不同版本的 Safari 中加载不完全导致我几乎抓狂(Safari 确实是新的 IE)的所有症状都已通过 Apache“配置黑客”为我们解决了。

I know this is an old topic, but I wanted to share a solution for Safari that might save others some time. The following line really solved all problems:

BrowserMatch "^(?=.*Safari)(?=.*Macintosh)(?!.*Chrom).*" nokeepalive gzip-only-text/html

The regex makes sure only Safari on Mac is detected, and not Mobile Safari and Chrome(ium) and such. Safari for Windows is also not matched, but the keepalive problem seems to be a Mac-Safari combination only. In addition, some Safari versions do not handle gzipped css/js well.

All our symptoms of our site crashing or CSS not completley loading in different versions of Safari which caused me to nearly pull my hair out (Safari really is the new IE) have been solved for us with this Apache 'configuration hack'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文