ajax json,xml,文本数据传递实例

作者在 2008-08-25 09:09:07 发布以下内容
到最后我才发现微软给的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里面可能就不行了。

查看:

http://hi.baidu.com/bb3852/blog/item/6972c7d18d4393d5562c844c.html

ajax | 阅读 5925 次
文章评论,共0条
游客请输入验证码
浏览56561次