jeffy

          BlogJava 首頁 新隨筆 聯系 聚合 管理
            70 Posts :: 1 Stories :: 14 Comments :: 0 Trackbacks
            當用js的alert 方法顯示ajax以responseText顯示返回結果時候, 顯示的是個xml結構文檔, 但以responseXML解析xml的時候, 所有節點長度都為0,  這個問題關鍵是服務器端沒有指定正確的文檔格式:
                  response.setContentType("text/xml;charset=UTF-8"); (正確)
                   response.setContentType("text/html;charset=UTF-8");(錯誤)
          posted on 2007-08-17 17:35 Live-in Java 閱讀(2986) 評論(2)  編輯  收藏

          評論

          # re: Ajax以responseXML返回,客戶端(IE)不能分析xml問題 2009-02-10 06:07 liyaxi
          原文:http://jiangx.javaeye.com/blog/153066
          1.對XMLHttpRequest請求返回的responseXML進行解析,responseXML是個XMLDOcument對象
          假設返回的responseXML為:
          <?xml version="1.0" encoding="UTF-8"
          standal?>
          <response>
          <method>checkName</method>
          <result>1</result>
          </response>
          則獲取method和result值方法為:
          var response = req.responseXML.documentElement;
          method = response.getElementsByTagName('method')[0].firstChild.data;
          result = response.getElementsByTagName('result')[0].firstChild.data;

          2.創建一個XMLDocument對象
          function getXMLDocument() {
          var xDoc = null;
          if (document.implementation && document.implementation.createDocument) {
          xDoc = document.implementation.createDocument("", "", null);
          } else {
          if ((typeof ActiveXObject) != "undefined") {
          var msXmlAx = null;
          try {
          msXmlAx = new ActiveXObject("Msxml2.DOMDocument");
          }
          catch (e) {
          msXmlAx = new ActiveXObject("Msxml.DOMDocument");
          }
          xDoc = msXmlAx;
          }
          }
          if (xDoc == null || typeof xDoc.load == "undefined") {
          xDoc = null;
          }
          return xDoc;
          }

          3.創建一個DOM樹
          <people>
          <person first-name="eric" middle-initial="h" last-name="jung">
          <address street="321 south st" city="denver" state="co" country="usa" />
          </person>
          <person first-name="jed" last-name="brown">
          <address street="321 north st" city="atlanta" state="ga" country="usa" />
          <address street="321 south avenue" city="denver" state="co" country="usa" />
          </person>
          </people>
          程序如下:
          var doc=getXMLDocument();
          var peopleElem = doc.createElement("people");
          var personElem1 = doc.createElement("person");
          personElem1.setAttribute("first-name", "eric");
          personElem1.setAttribute("middle-initial", "h");
          personElem1.setAttribute("last-name", "jung");

          var addressElem1 = doc.createElement("address");
          addressElem1.setAttribute("street", "321 south st");
          addressElem1.setAttribute("city", "denver");
          addressElem1.setAttribute("state", "co");
          addressElem1.setAttribute("country", "usa");
          personElem1.appendChild(addressElem1);

          var personElem2 = doc.createElement("person");
          personElem2.setAttribute("first-name", "jed");
          personElem2.setAttribute("last-name", "brown");

          var addressElem3 = doc.createElement("address");
          addressElem3.setAttribute("street", "321 north st");
          addressElem3.setAttribute("city", "atlanta");
          addressElem3.setAttribute("state", "ga");
          addressElem3.setAttribute("country", "usa");
          personElem2.appendChild(addressElem3);

          var addressElem5 = doc.createElement("address");
          addressElem5.setAttribute("street", "321 south avenue");
          addressElem5.setAttribute("city", "denver");
          addressElem5.setAttribute("state", "co");
          addressElem5.setAttribute("country", "usa");
          personElem2.appendChild(addressElem5);

          peopleElem.appendChild(personElem1);
          peopleElem.appendChild(personElem2);
          doc.appendChild(peopleElem);
          alert(doc.xml);//xml屬性只對IE管用
            回復  更多評論
            

          # re: Ajax以responseXML返回,客戶端(IE)不能分析xml問題 2009-02-10 06:20 liyaxi
          得到的是一個數組
          var str=new Array();
          var xmlDoc=xmlHttp.responseXML;
          for(var i=0;i<xmlDoc.length;i++)
          {
          str[i]=xmlDoc.getElementsByTagName("name")[i].childNodes [0].nodeValue;

          }
          注意要判斷ie text() 和火狐的textContent()區別  回復  更多評論
            


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 临湘市| 安达市| 永顺县| 渝中区| 迭部县| 和平县| 灌云县| 庆云县| 铜梁县| 叶城县| 赞皇县| 定襄县| 获嘉县| 合江县| 靖远县| 新竹市| 临汾市| 治多县| 甘肃省| 津市市| 永春县| 中西区| 和林格尔县| 金溪县| 全椒县| 凤城市| 绥德县| 泰和县| 麦盖提县| 平果县| 厦门市| 锡林郭勒盟| 五家渠市| 射洪县| 吕梁市| 文昌市| 日土县| 泾源县| 兴隆县| 新巴尔虎右旗| 旬邑县|