问题
I seem to be having a problem with long polling and IE. This is my first foray into long polling, so I set up a simple test to see if I could make it work. It seems to behave just fine with FF and Chrome, but I'm getting different results with IE.
First, here's some code:
HTML/Javascript:
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script type="text/javascript" src="js/jquery-1.8.3.min.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.9.2.min.js"></script>
<script type="text/javascript">
//<![CDATA[
$(document).ready(function()
{
(function poll()
{
$.ajax({
url: 'events/alert-data.php',
success: function (e)
{
$('#results').append($('<div>Success: ' + e.text + '</div>').fadeIn(1000));
},
error: function (e)
{
console.log(e);
},
dataType: 'json',
complete: poll,
timeout: 10000
});
})();
});
//]]>
</script>
</head>
<body>
<div id="results">hello</div>
</body>
</html>
PHP:
<?php
$time = time();
while (time() - $time < 5) { }
echo json_encode(array('text' => time()));
?>
In FF/Chrome, I'm seeing expected data:
hello
Success: 1356104196
Success: 1356104201
Success: 1356104217
Success: 1356104222
Success: 1356104227
But in IE it repeats the first Success
line infinitely. At least I presume it's infinite as it locks up the browser and won't allow me to scroll.
I'm not sure what I'm doing wrong. Any help would be much appreciated.
Thanks in advance.
回答1:
The problem with IE would appear to be the consequence of caching, probably by IE itself. This could potentially happen in any browser.
Try adding :
cache: false
to the ajax options.
来源:https://stackoverflow.com/questions/13993056/ajax-long-polling-not-working-with-ie