DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> XML學習教程 >> XML詳解 >> 一個由字符引起的XML加載錯誤
一個由字符引起的XML加載錯誤
編輯:XML詳解     
sp;    今天看到一篇介紹AJax原理的文章(http://www.dotnetbips.com/bdb52d60-1100-43b0-9b80-b765937201e8.aspx?articleid=306 ),主要是舉了一個例子來說明。看了一下也就是關於XMLHTTP的應用,於是自己照著CODE一下。很快寫完,於是測試,看看效果,本來以為可以馬上順利的看到效果,在下拉框裡隨便選了兩項,一切ok,要取得的數據也正常能正常顯示出來,但是在選另一下項的時候報錯了。說是,webform1.ASPx的腳本裡頭var orders=dstRoot.getElementsByTagName("OrderID"); 'null'為空或不是對象

       趕快檢查一下又沒有把代碼敲錯(不可能照著敲也出錯呀!)果然,沒有敲錯!代碼邏輯上也是完全沒有問題的,為什麼會報這麼一個錯?!

       最後幾經周折,比較,發現在Northwind這個數據庫Orders表中,有的記錄中有這樣的內容México D.F. 注意到這個é了麼!就是這個字符的原因導致報錯,也不知道是哪國字符,索性把é改成e,終於天下太平了!

       下面附上代碼,要是遇到這方面應用的時候,這些代碼還是比較有用的!

Webform1.ASPx的<HEAD>中:

              <script language="Javascript">

              var obj;

              function GetDataViaAJax()

              {

                try

                {

                  obj=new ActiveXObject("Msxml2.XMLHTTP");

                }

                catch(e1)

                {          

                  try

                  {

                    obj=new ActiveXObject("Microsoft.XMLHTTP");

                  }

                  catch(e2)

                  {

                    obj=null;

                  }

                }

                if(obj!=null)

                {

                  obj.onreadystatechange=ProcessResponse;

                  obj.open("Get","./WebForm2.ASPx?customerid="+document.getElementById("Dropdownlist1").value,true);

                  obj.send(null);

                }

                return false;

              }

              function ProcessResponse()

              {

                if(obj.readyState==4)

                {

                  if(obj.status==200)

                  {

                    var dstRoot=obj.responseXML.documentElement;

                    var ddlOrders=document.getElementById("ListBox1");

                 for(var i=ddlOrders.length-1;i>-1;i--)

                    {

                      ddlOrders.options[i]=null;

                    }

                    var orders=dstRoot.getElementsByTagName("OrderID");

                    var text;

                    var listItem;

                    for(var i=0;i<orders.length;i++)

                    {

                      text=(orders[i].textContent||orders[i].innerText||orders[i].text);

                      listItem=new Option(text,text);

                      ddlOrders.options[i]=listItem;

                    }

                  }

                  else

                  {

                    alert(obj.status);

                    alert("Error Retriving Data!");

                  }

                }

              }

              </script>

<form>中:

              <form id="Form1" method="post" runat="server">

                     <FONT face="宋體">

                            <P>

                                   <asp:DropDownList id="DropDownList1" runat="server" Width="136px"></ASP:DropDownList></P>

                            <P>

                                   <asp:ListBox id="ListBox1" runat="server" Width="136px"></ASP:ListBox></P>

                     </FONT>

              </form>

CODEBEHIND:

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        '在此處放置初始化頁的用戶代碼

        If Not IsPostBack Then

            Dim dst As New DataSet

            Dim conn As New SqlClIEnt.SqlConnection("server=localhost;user id=sa;passWord=ycm119;database=northwind;")

        ;    Dim dad As New SqlClIEnt.SqlDataAdapter("Select customerid,companyname from customers", conn)

            dad.Fill(dst)

            DropDownList1.DataSource = dst

            DropDownList1.DataTextFIEld = "companyname"

            DropDownList1.DataValueFIEld = "customerid"

            DropDownList1.DataBind()

        End If

        DropDownList1.Attributes.Add("onchange", "return GetDataViaAJax()")

End Sub

Webform2.ASPx中:

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        '在此處放置初始化頁的用戶代碼

        If Not IsPostBack Then

            Dim conn As New SqlClIEnt.SqlConnection("server=localhost;user id=sa;passWord=ycm119;database=northwind;")

            Dim dad As New SqlClIEnt.SqlDataAdapter("Select * from orders where customerid='" & Request.QueryString("customerid") & "'", conn)

            Dim dst As New DataSet

            dad.Fill(dst, "customers")

            Response.Clear()

            Response.ContentType = "text/XML"

            Response.Write(dst.GetXML)

            Response.End()

        End If

    End Sub

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved