【发布时间】:2017-07-25 05:57:50
【问题描述】:
我正在尝试获得一个非常基本的长轮询框架以跨浏览器工作。我的解决方案在 Chrome 中运行良好,但无法在 Edge 中运行。
在 Edge 中,页面加载没有错误,我可以看到(Edge 中的 F12)。但是,服务器没有收到任何请求(与 Chrome 不同)。浏览器开发人员工具的网络选项卡上显示的唯一流量是 jQuery 本身的下载。我可以直接浏览到我希望轮询的 URL 并获得结果。我添加了一些控制台日志记录,这就是我在 Edge 中看到的:
IIFE
Setting up polling
>> Polling
<< Polling
就是这样。在 Chrome 上,最后两行会无限重复。
更新:我将代码和服务器从我的 PC 移到了开发服务器上。在这种环境下,代码可以在 Chrome 和 Edge 上运行。我可以在 Edge 中看到网络流量并得到服务器的响应。所以看来这个问题与我的本地环境有关,而不是 javascript 本身。
这是我的基本代码:
index.html:
<!DOCTYPE html>
<html>
<head>
<title>AJAX Polling Exploration</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript" src="poll.js"></script>
</head>
<body>
<div id="wrapper">
...
</div>
</body>
</html>
poll.js:
$(function() {
console.log('IIFE');
// Make sure caching is off as many suggest this is required for IE
$.ajaxSetup ({
cache: false
});
function polling() {
console.log('>> polling');
$.ajax({
type: 'GET',
url: 'http://localhost:8080/result',
cache: false,
data: { name: 'Test', date: new Date().getTime() }, // Make sure the payload varies - caching again
success: function(data, textStatus, jqXHR) {
// Take action ...
//setTimeout(polling, 2000); // Apparently this is no good for IE
setTimeout(function(){ polling(); }, 2000);
}
});
console.log('<< polling');
}
//polling();
console.log('Setting up polling');
setTimeout(function(){ polling(); }, 2000);
});
关于这个主题有很多类似的问题,我听从了他们的建议,但无济于事。具体来说: (a) 确保 IE 的缓存已关闭; (b) 使用正确的 setTimeout() 格式。
应该无关紧要,但以防万一,localhost:8080 的应用程序是基于 Spring 并在 Tomcat 下运行的。
(注意,最初我的问题是说我无法在 IE 中使用它。我发现我无意中使用了旧版本的 jQuery。从 Google CDN 切换到最新版本可以在 IE 中运行。)
【问题讨论】:
-
I cannot get it to work- 解决方案是修复它 - 听起来很简单,但如果没有对问题的有用描述,您将不会得到有用的答案:p -
@JaromandaX 好点,观察到的行为丢失了。我现在已经添加了。
-
no request is received at the server- 你能观察到客户端正在发送请求吗? -
@JaromandaX 我不确定。看到这个最简单的方法是什么?
-
浏览器开发者工具网络标签
标签: javascript jquery ajax internet-explorer long-polling