日期:2014-05-19  浏览次数:20693 次

IE下ajax只能响应一次
功能是这样的,前台ajax每隔一秒向服务器发送请求,并且将返回的数据在网页中显示出来。但是在IE下面只有第一次的请求能正常返回,之后的请求服务器是接收到了请求,但是似乎前台得不到响应。

火狐下没问题,一切OK,但是在IE下就有问题,网上查了下可能是缓存问题,但是所有的解决方案都试过了还是不行。
代码如下:
HTML code

<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <script type="text/javascript" src="json2.js"></script>
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
    <script type="text/javascript">
    var xmlhttp;
    var length = 0;
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
        function query(){
            json();
            var t = setTimeout("query()", 1000);  //每隔一秒向服务器发送请求
        }
        function json(){
            xmlhttp.onreadystatechange=function(){
                alert(xmlhttp.readyState + " " + xmlhttp.status);  //只有第一次能正常响应
                if (xmlhttp.readyState==4 && xmlhttp.status==200)
                {
                    var query = "";
                    var i = 0;
                    var result = eval(xmlhttp.responseText);
                    if(length < result.length){
                    for(;i < result.length; i++){
                        query = query + result[i].name + ":" + result[i].text + "\n";
                    }
                    
                    document.getElementById("query").value = query;
                    length = result.length;
                    }
                }}
                
            
            xmlhttp.open("GET","/Chat/json?time="+new Date(),true);
            xmlhttp.send();
            
                            
        }

        function add(){
            var name = document.getElementById("name").value;
            var text = document.getElementById("text").value;
            xmlhttp.onreadystatechange=function()
              {
              if (xmlhttp.readyState==4 && xmlhttp.status==200)
                {
                    
                }
              }
            xmlhttp.open("GET","/Chat/json?name="+name+"&text="+text+"&mode=add&time="+new Date(),true);
            xmlhttp.send();
        }
    </script>
  </head>
  
  <body onload="query()">
    <textarea rows="5" cols="30" id="query"></textarea>
        <input type="text" id="name"> 
        <textarea rows="5" cols="30" id="text" ></textarea>
        <input type="button" value="提交" onclick="add()">
  </body>
</html>