昨天看了ajax,上周也听了师兄们的json等一些讲解,大致有了一些了解,我就写一些 ajax json 实例给一些初学ajax的朋友们一些指导,对于ajax.dll的使用我就不说了,我感觉对于ajax.dll使用是比较方便的,但是本质的东西还是有必要了解会用的。我到网上搜索ajax json 实例,找了很久都没找到一些有用的东西,微软的倒是给了一些实例但是他们的实例只是对于ie浏览器有点用,用到firefox火狐浏览器上面我这是晕了。到最后我才发现微软给的ajax json 实例都是有问题的,很多都是不严密的,特别是对于大小写方面,他们都没有仔细追究大小写问题,导致了在firefox使用有问题。下面是实例内用:两个html之间的:

<head> <title>测试ajax</title> <meta http-equiv=”Content-Type” content=”text/html;charset=utf-8″ />

<script type=”text/javascript”>

var xmlHttp=null;

 function creatXMLhttp()

 {

        if(window.ActiveXObject)

        {

                  xmlHttp=new ActiveXObject(”Microsoft.XMLhttp”);

        }

        else

        {

                  xmlHttp=new XMLHttpRequest();

         }

}

creatXMLhttp();

function sendAjax(method,url,func)

{

      //xmlHttp.setRequestHeader(”Cache-Control”,”no-cache”);

     //xmlHttp.setRequestHeader(”If-Modified-Since”,”0″);

     xmlHttp.open(”GET”,url+”?rnd=145236″+Math.random(),true);

     xmlHttp.send(null);

     xmlHttp.onreadystatechange=func;

}

function xmlHttpReadXML()

{

     if (xmlHttp.readyState==’4′ || xmlHttp.readyState==’complete’)

     {

            var xmlDoc=xmlHttp.responseXML;

            var xmlconent=xmlDoc.getElementsByTagName(”item”);

            var xmlname=xmlconent[0].getElementsByTagName(”name”)[0].firstChild.data;

           var xmlemail=xmlconent[0].getElementsByTagName(”email”)[0].firstChild.data;

            document.getElementById(”stuinfo”).innerHTML=”<p>姓名:”+xmlname+”</p>邮箱:”+xmlemail; } 

     else

     {

             document.getElementById(”stuinfo”).innerHTML=”<p>正在读取数据</p>”;

      }

}

function xmlHttpReadTEXT()

 {

      if (xmlHttp.readyState==’4′ || xmlHttp.readyState==’complete’)

      {

                 var xmlText=xmlHttp.responseText.split(”$”)[1]; document.getElementById(”stuinfo”).innerHTML=”<p>”+xmlText+”</p>”;

     }

     else

     {

                document.getElementById(”stuinfo”).innerHTML=”<p>正在读取数据</p>”;

      }

}

function xmlHttpReadJSON()

{

      alert(”123″);

       if (xmlHttp.readyState==’4′ || xmlHttp.readyState==’complete’)

       {

               var xmlText=xmlHttp.responseText.split(”$”)[1];

               eval(”var res=(”+xmlText+”)”);

               document.getElementById(”stuinfo”).innerHTML=”<p>姓名:”+res.name+”</p>邮箱:”+res.email;

         }

         else

         {

                   document.getElementById(”stuinfo”).innerHTML=”<p>正在读取数据</p>”;

          }

}

</script>

</head>

<body>

 <a href=”#” onclick=”sendAjax(’GET’,'xml/1.xml’,xmlHttpReadXML)”>读取xml测试</a>

<a href=”#” onclick=”sendAjax(’POST’,'1.aspx’,xmlHttpReadTEXT)”>POST测试</a>

<a href=”#” onclick=”sendAjax(’GET’,'1.aspx’,xmlHttpReadJSON)”>JSON测试</a>

<div id=”stuinfo”> </div>

</body>

xml文件如下: <?xml version=”1.0″ encoding=”utf-8″?> <root> <item> <name>张三</name> <email>lll@163.com</email> </item> </root>

1.aspx.cs文件如下:

protected void Page_Load(object sender, EventArgs e) { Response.Write(”${name:’张三’,email:’google@gmail.com’}$”); }

提醒几点:解读Response的那里面多出的$不要小看,不要忘记了,不然在firefox里面不能正常运行。 readyState responseText responseXML中的大小写别搞错了,ie里面可能没有错误,在firefox里面可能就不行了。