最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - AJAX problem in IE9? - Stack Overflow

programmeradmin4浏览0评论

I have made an AJAX chatroom; and it works in chrome and FF, but of course, not in IE. Here's my code:

<script language="javascript" type="text/javascript">
<!-- 
//Browser Support Code
function ajaxFunction(){
    var ajaxRequest; 
    try {
      ajaxRequest = new XMLHttpRequest();
    } catch (e) {
      try {
        ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
         try {
           ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (e){
           alert("Your browser broke!");
           return false;
     }
  }
    }

    ajaxRequest.onreadystatechange = function(){
      if(ajaxRequest.readyState == 4) {
        var ajaxDisplay = document.getElementById('ajaxDiv');
        ajaxDisplay.innerHTML = ajaxRequest.responseText;
      }
    }

    ajaxRequest.open("GET", "pull.php", true);
    ajaxRequest.send(null);  
}

setInterval( "ajaxFunction()", 1000 );

//-->
</script>

The result never displays. I have a div named AjaxDiv if that helps anyone. What am I doing wrong? Is this a bug?

I have made an AJAX chatroom; and it works in chrome and FF, but of course, not in IE. Here's my code:

<script language="javascript" type="text/javascript">
<!-- 
//Browser Support Code
function ajaxFunction(){
    var ajaxRequest; 
    try {
      ajaxRequest = new XMLHttpRequest();
    } catch (e) {
      try {
        ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
         try {
           ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (e){
           alert("Your browser broke!");
           return false;
     }
  }
    }

    ajaxRequest.onreadystatechange = function(){
      if(ajaxRequest.readyState == 4) {
        var ajaxDisplay = document.getElementById('ajaxDiv');
        ajaxDisplay.innerHTML = ajaxRequest.responseText;
      }
    }

    ajaxRequest.open("GET", "pull.php", true);
    ajaxRequest.send(null);  
}

setInterval( "ajaxFunction()", 1000 );

//-->
</script>

The result never displays. I have a div named AjaxDiv if that helps anyone. What am I doing wrong? Is this a bug?

Share Improve this question edited May 13, 2011 at 19:19 phihag 289k75 gold badges469 silver badges483 bronze badges asked May 13, 2011 at 19:17 user569322user569322 10
  • 4 Or you could use jquery/mootools, which'd reduce all that to about 2 lines. Rolling your own ajax handlers these days is too painful. – Marc B Commented May 13, 2011 at 19:19
  • ive considered that option, but im more concerned if microsoft made a bug in such a widely-used product – user569322 Commented May 13, 2011 at 19:21
  • 6 @Ken programmer.97things.oreilly./wiki/index.php/… – phihag Commented May 13, 2011 at 19:23
  • 1 @Ken Can you provide a full working example? I copied your code and it works fine in IE9 (Windows7 x86): phihag.de/2011/so/ie9-ajax.html . And IE8 had quite a share of bugs, you may inadvertently send buggy IE8 code to IE9 too. – phihag Commented May 13, 2011 at 19:30
  • 1 Post an example to jsfiddle jsfiddle is your friend – user120242 Commented May 13, 2011 at 19:32
 |  Show 5 more ments

1 Answer 1

Reset to default 5

Probably yanking out a cached copy every time you make a request.

Either set the correct caching headers on the server

header( 'Cache-Control: no-store, no-cache, must-revalidate' );
header( 'Pragma: no-cache' ); 

Or append a query string to the get request like the following

ajaxRequest.open("GET", "pull.php?ts=" + new Date().getTime(), true);
发布评论

评论列表(0)

  1. 暂无评论