用Ajax实现如下功能:    在一个页面利用ajax每隔3秒向服务器端查询一次数据库(后台页面Find_Support_Answer.aspx中进行),看是否有指定的记录添入,查到了就返回该记录关键字,在返回信息的处理函数中再次利用ajax发送HttpRequest到另一个后台页面:Dialog_Initialize.aspx 将一些初始化信息写入数据库中,在这一次的返回信息处理函数中最后导航到页面Custom_Support_Chat_Custom.aspx代码如下:<script language="javascript">
        var http_request = false;
        var l_TotalSeconds = 0;
        var l_MYID = "";
        var l_ComID = "";   
        var l_TimeLine = "";             
        var l_DialogTopic = "";
        var l_SupportID = "";
        var l_DialogID = "";
        var l_DialogInitiator = "";
        function send_request(url,readystatechange) 
        {   
            //初始化、指定处理函数、发送请求的函数
            http_request = false;
            //开始初始化XMLHttpRequest 对象
            if(window.XMLHttpRequest)
            { //Mozilla 浏览器
                http_request = new XMLHttpRequest();
                if (http_request.overrideMimeType)
                {//设置MiME 类别
                    http_request.overrideMimeType("text/xml");
                }
            }
            else if (window.ActiveXObject)
            { // IE 浏览器
                try
                {
                     http_request = new ActiveXObject("Msxml2.XMLHTTP");
                }
                catch (e)
                {
                    try
                    {
                        http_request = new ActiveXObject("Microsoft.XMLHTTP");
                    }
                    catch (e) {}
                }
            }
            if (!http_request) 
            { //异常,创建对象实例失败
                window.alert("不能创建XMLHttpRequest 对象实例.");  
                return false;
            }
            http_request.onreadystatechange = readystatechange;
            //确定发送请求的方式和URL 以及是否同步执行下段代码
            http_request.open('POST', url, true);
            http_request.send(null);
        }
        
// 处理返回信息的函数
function processRequest() 
{
    if (http_request.readyState == 4) 
    { // 判断对象状态
        if (http_request.status == 200) 
        { // 信息已经成功返回,开始处理信息
            var l_newMessage = http_request.responseText;
            if ((l_newMessage != "")&&(l_newMessage != null))    
            {
                //l_TotalSeconds = 1000;
                clearInterval(l_Interval);
                l_MYID = document.getElementById("Text2").value.toString();
                l_ComID = document.getElementById("Text1").value.toString();   
                l_TimeLine = document.getElementById("Text3").value.toString();             
                l_DialogTopic = document.getElementById("Text4").value.toString();
                l_SupportID = l_newMessage;
                //拼出dialogID
                l_DialogID = l_ComID + l_SupportID + l_MYID + l_TimeLine;
                document.getElementById("Text5").value = l_DialogID;
                l_DialogInitiator = "Custom";                
                //由于已经停止了循环,可以使用同一个ajax引擎把对话开始信息写入数据库
                document.getElementById("Panel14").style.display = "inline";
                document.getElementById("Label10").innerText = "客服已应答!对话初始化中...";
                setTimeout ('InitializeDialog();',2000);
            }
        }
        else
        { //页面不正常
            alert("您所请求的页面有异常。");
        }
    }
}function a()
{
    
}
 
function GetNewMessage()
{
        if(l_TotalSeconds <= 60)
        {            
            l_TotalSeconds ++;            
            document.getElementById("Label9").innerText = l_TotalSeconds;
            //45秒以内,字为绿色
            //45秒后,字为红色
            if(l_TotalSeconds <= 45)
            {
                document.getElementById("Label9").style.color = "lightgreen";
            }
            else
            {
                if(45 < l_TotalSeconds <= 60)
                {
                    document.getElementById("Label9").style.color = "red";
                }
               
            }      
            if(l_TotalSeconds%3==0)
            {              
               l_MYID = document.getElementById("Text2").value.toString();
               l_ComID = document.getElementById("Text1").value.toString();
               l_TimeLine = document.getElementById("Text3").value.toString();
               send_request('Find_Support_Answer.aspx?MyID=' + l_MYID + '&CompanyID=' + l_ComID + "&TimeLine=" + l_TimeLine,processRequest);
            }
        }
        else
        {
            document.getElementById("Panel12").style.display = "inline";
            document.getElementById("Panel13").style.display = "inline";
        }
}var l_Interval = setInterval("GetNewMessage()", 1000);function InitializeDialog()
{
    var l_MYID = document.getElementById("Text2").value.toString();
    var l_ComID = document.getElementById("Text1").value.toString();
    // var l_TimeLine = document.getElementById("Text3").value.toString();
    send_request('Dialog_Initialize.aspx?CompanyID=' + l_ComID + "&SupportID=" + l_SupportID + "&SessionID=" + l_MYID + "&DialogID=" + l_DialogID + "&DialogInitiator=" + "Custom",processRequest1);
}function processRequest1() 
{
    if (http_request.readyState == 4) 
    { // 判断对象状态
        if (http_request.status == 200) 
        { // 信息已经成功返回,开始处理信息
            var l_newMessage = http_request.responseText;
            if ((l_newMessage != "")&&(l_newMessage != null))    
            {
                if (l_newMessage == "初始化对话成功!稍后将进行对话...")
                {
                    document.getElementById("Label10").innerText = l_newMessage;
                    l_DialogID = document.getElementById("Text5").value;
                    setTimeout('a();',2000);              
                    window.open("Custom_Support_Chat_Custom.aspx?CompanyID=" + l_ComID + "&MyID=" + l_MYID + "&DialogID=" + l_DialogID + "&SupportID=" + l_SupportID + "&DialogTopic=" + l_DialogTopic,'chatbox','toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,top=50,left=50,width=800,height=500');                          
                    window.close();
                }
                else
                {
                    document.getElementById("Label10").innerText = "对话初始化失败!窗口将自动关闭!";
                    setTimeout('window.close();',2000);
                }
            }
        }
        else
        { //页面不正常
            document.getElementById("Label10").innerText = "对话初始化过程异常,对话将自动结束!请谅解!";
            setTimeout('window.close();',2000);
        }
    }
}
     
</script>
整个代码从SetInterView开始现在问题是在标红的那一行有一个参数l_DialogID,这个参数传不到后台页面去,在Custom_Support_Chat_Custom.aspx.cs中利用Request.QueryString["DialogID"]去获取的结果为""
请问各位大哥这是怎么回事。小弟我百思不得其解......

解决方案 »

  1.   

    晕,标红居然不显示 就是下面这一行 
    window.open("Custom_Support_Chat_Custom.aspx?CompanyID=" + l_ComID + "&MyID=" + l_MYID + "&DialogID=" + l_DialogID + "&SupportID=" + l_SupportID + "&DialogTopic=" + l_DialogTopic,'chatbox','toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,top=50,left=50,width=800,height=500'); 代码的倒数第19行
      

  2.   

    也许queryString带有中文或者什么什么的,escape一下试试.
      

  3.   

    你必须确认在你传之前 l_DialogID 中是有值的,你把Window.open()的那个url,弹出来看看,是不是url的问题
      

  4.   

    断点调试表明 在 window.open()的前一行l_DialogID还是有值的,值为:"70729fengling127.0.0.12008-10-30 15:14:15"
      

  5.   

    找到问题了,多谢各位了
    这位lizhiming0310在QQ上为我解决了很久,所以把大部分分分给他