VS2005写的一个简单ajax网页,在IE环境下运行正常,在firefox无反应
代码如下:
default.aspx
<%@ page language="C#" autoeventwireup="true" inherits="_Default, App_Web_bmocc8og" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
    <link href="cssdiv.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript">
        var xmlHttp;
        
        function createXMLHttpRequest()
        {
            if (window.ActiveXObject)
            {
             try
             {
                 xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch (e1)
                {
                 try
                 {
                 xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
                 }
                 catch (e2)
                 {
                 alert("创建xmlhttp失败");
                 }
                }
            }
            else if (window.XMLHttpRequest)
            {
                xmlHttp = new XMLHttpRequest();
            }
            //alert(xmlHttp);
        }        function getSolutions(tranid)
        {
            var tip = document.getElementById(tranid);
            tip.innerHTML = "计算中";
            url="getData.aspx?tranid=" + tranid;
            xmlHttp.open("POST", url, true);
            xmlHttp.onreadystatechange = doUpdate;
            xmlHttp.send();
            return false;
        }
        
        function doUpdate()
        {
            if (xmlHttp.readyState==4)
            {
                document.getElementById('TOT').value = xmlHttp.responseText;
                var solutions = xmlHttp.responseText.split("\t");
                tranid = solutions[0];
                tip = document.getElementById(tranid);
                tip.innerHTML = solutions[1];
            }
        }
        
        //控制tip显示
        function showtip(linker, divid)
        {
            //alert(document.getElementById(divid).style.left);
            //document.getElementById(divid).style.left = 100px;//(document.getElementById(linker).innerHTML.length) + "px";
            
            /*
            if ((document.getElementById(divid).innerText.length)!=0)
            {
                
            }
            */
            document.getElementById(divid).style.display = "block";
            getSolutions(divid);
        }
        function hidetip(divid)
        {
            document.getElementById(divid).style.display = "none";
        }
        
    </script>
</head>
<body onload = "createXMLHttpRequest();">
    <form id="form1" runat="server">
    <div id = "container">
        <div id = "header">
            <div id = "menu">
                <ul>
                    <li><a href = "Default.aspx">首页</a></li>
                    <li class="menudiv"></li>
                    <li>退出</li>
                </ul>
            </div>
        </div>
        <div id = "pagebody">
            <div id = "mainbody">
            <a id = "hreftest" href = "ShowDetail.aspx" onmouseover="showtip('hreftest','4');" onmouseout = "hidetip('4');">4</a><br />
            <div id = "4" class = "tipdiv">获取内容中...</div>
            </div>
        </div>
    </div>
    </form>
</body>
</html>getData.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;public partial class getData : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        int tranId = 0; //IE可以正常运行到这里,firefox不行
        if (Request.QueryString["tranid"] != null)
        {
            tranId = Convert.ToInt32(Request.QueryString["tranid"].ToString());
            Response.Write(tranId.ToString() + "\t" + DRCOM.Business.Solutions.GetSolutions(tranId) + "\t");
        }
    }
}运行default.aspx,鼠标移动到超链接上,适用ajax获取后台数据库的内容。
ie7完全正常,firefox不行,没有任何反应。

解决方案 »

  1.   

    var xmlhttp;
    try{
    xmlhttp=new XMLHttpRequest();
    }
    catch(e){
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
            xmlhttp.onreadystatechange=function(){
    if (xmlhttp.readyState==4){
        if (xmlhttp.status==200){
                  //
                }
    }
    }
      

  2.   

    应该是前台JS的问题,你用firebug调试一下看看
      

  3.   


    我试过了,不是顺序的问题,在firefox里面确实是用xmlHttp = new XMLHttpRequest()这个创建的。
      

  4.   


    太诡异了,我开了firebug,提示uncaught exception: ?(¹Õ Location.toString öCP ³
    但是可以正常拿到数据了,重新刷新页面后,没有任何错误,运行也正常。实在太诡异了。