问个奇怪的问题。下面是ajax的代码...
<script language="javascript" type="text/javascript">
var xmlHttp;
var popWindow;
function createXMLHttpRequest()
{
 if(window.ActiveXObject)
 {
   xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
 }
 else if(window.XMLHttpRequest)
 {
   xmlHttp=new XMLHttpRequest();
 }
}
function doRequestUsingGET()
{
    createXMLHttpRequest();
     var queryString="Pop/CheckNewCaller.aspx?Channel=<%=Channel %>";
     //alert(queryString);
     xmlHttp.onreadystatechange=handleStateChange;
     xmlHttp.open("GET",queryString,true);
     xmlHttp.send(null);
}
function handleStateChange()
{
 if(xmlHttp.readyState==4)
 {
  if(xmlHttp.status==200)
  {
            if(xmlHttp.responseText!="a")
            {
                popWindow= window.open("Pop/PopWindow.aspx?Phone=" + xmlHttp.responseText,'CCPop',"width=600,height=419,left=200,top=100");
                popWindow.focus();
            }
  }
 }
}
function OpenWindow()
{
    
}
setInterval("doRequestUsingGET()",5000);
</script>
这是访问的:CheckNewCaller.aspx的代码。
protected void Page_Load(object sender, EventArgs e)
    {
        if (Application["Caller"] != null && Request.QueryString["Channel"] != null)
        {
            DataTable dt = ((DataSet)Application["Caller"]).Tables["Caller"];
            string channel = Request.QueryString["Channel"].ToString();            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    if (dr["Channel"].ToString() == channel)
                    {
                        try
                        {
                            Response.Write(dt.Rows[0]["Phone"].ToString());
                            dt.Rows.Remove(dr);
                            Response.End();
                        }
                        catch
                        {
                            //Response.Write(new Random().Next(10));
                            Response.Write("a");
                            Response.End();
                        }                    }
                }
            }
            else
            {
                //Response.Write(new Random().Next(10, 100));
                Response.Write("a");
                Response.End();
            }
        }
        else
        {
            //Response.Write(new Random().Next(100, 1000));
            Response.Write("a");
            Response.End();
        }
    }
问题是这样的,如果CheckNewCaller.aspx里的代码写出随机数,那么ajax就会一直执行。如果只写出a,那么ajax执行一次就停了。为什么呀?????????????

解决方案 »

  1.   

    以上代码并没有错,,我测试了一下.
    Response.Write("a");
    或者Response.Write(new Random().Next(100, 1000)); 都是没有问题的.只是Response.Write(new Random().Next(100, 1000)); 前台会执行window.open()..所以可以感觉出来
    而只输出a前台是没有任何反应的.所以就造成了没有执行的错觉.你可以试试
    if(xmlHttp.responseText!="a") 
                { 
    alert("rand");
                    popWindow= window.open("Pop/PopWindow.aspx?Phone=" + xmlHttp.responseText); 
                    popWindow.focus(); 
                } 
    else
    {
    alert("a");
    }这样不管你是输出随机还是a都可以看出来..
      

  2.   


    load里加上  Context.Response.Cache.SetCacheability(HttpCacheability.NoCache);这个是清除页面缓存,  其实我也用同样的疑问, 如果2次请求的地址一样,  浏览器就直接读取缓存的内容。这只是我的看发,希望了解的运行原理的人帮忙解释下。