Long polling jQuery doesn't work in IE
$.ajax seems to not work in IE. What should I do or it's just another bug in IE? Do I need to provide my code here to receive help? Because it seems it doesn't work with any $.ajax example.
My code:
function get_info(lines) {
$.ajax({
type: "POST",
cache: false,
url: "chat.php?RandomNumber=" + Math.random(),
data: "type=get_info&lines="+lines+"&RandomNumber=" + Math.random(),
dataType: "json",
success: function(msg){
lines = msg.lines;
if(msg.new_messages)
{
for(i =开发者_StackOverflow社区 0; i < msg.messages.length; i++)
{
$('.chat').append("<p>"+msg.messages[i]+"</p>");
}
document.getElementById('chatty').scrollTop = document.getElementById('chatty').scrollHeight;
}
},
complete: function() {
setTimeout(get_info, 1000, lines);
}
})
};
setTimeout(get_info, 1000, 0);
I now see that you're using a form of setTimeout that doesn't work with IE1, 2:
setTimeout(myFunction,myTimeout,parameter); //does NOT work for IE
Instead, use an anonymous function as the argument which should call the intended function with the correct argument:
setTimeout(function(){myFunction(myParameter);},myTimeout);
So your initial call to setTimeout should be changed to:
setTimeout(function(){get_info(0);}, 1000);
and subsequent calls on success should be:
setTimeout(function(){get_info(lines);}, 1000);
If this is because IE is caching your GET requests, you could simply set cache to false for jQuery.ajax() and let jQuery handle it for you (remember to clear your cache after making this change):
//do this for *all* ajax requests
$.ajaxSetup ({
cache: false
});
or
//do it for this ajax request
$.ajax ({
cache: false,
//..other options here
});
Add a timestamp in your data to get rid of the IE cache.
var timestamp = new Date();
$.ajax({
url: "/toto",
data: { ....., timestamp: timestamp.getTime() },
...
});
加载中,请稍侯......
精彩评论