[問題] responseXML為Null

看板Ajax作者 (Peter)時間14年前 (2012/06/04 03:42), 編輯推噓1(1012)
留言13則, 4人參與, 最新討論串1/1
大家好,我遇到一個問題 我在server端asp把Response.ContentType設成text/xml 然後用Response.write產生一個xml檔 程式如下 Response.ContentType="text/xml"; Response.write("<?xml version=1.0 encoding=utf-8?>\n"); Response.write("<?<recordNum>"+counter+"</recordNum>\n"); Response.write("<fieldCount>"+RS.Fields.Count+"</fieldCount>\n"); Response.write("<fieldName>\n"); for (i=0; i<RS.Fields.Count; i++) Response.Write("<field>"+RS(i).Name+"</field>\n"); Response.Write("</fieldName>\n"); 接著在Client端用Ajax request 去讀取這個xml檔 但是他會把上面這段內容存在responseText而不是responseXML responseXML則是NULL 請問有甚麼方法可以讓我正確的拿到XML檔嗎? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 118.169.220.201

06/04 09:56, , 1F
你<?印兩次
06/04 09:56, 1F

06/04 12:58, , 2F
我HEADER部分是另外印一個<? ?> 然後後面又有一個
06/04 12:58, 2F

06/04 12:59, , 3F
啊 懂了 謝謝!
06/04 12:59, 3F

06/04 14:35, , 4F
我拿掉了以後responseXML還是NULL耶
06/04 14:35, 4F

06/04 14:36, , 5F
然後responseText是我印的那些值
06/04 14:36, 5F

06/04 15:10, , 6F
您直接用browser看這個asp 會正確的顯示XML嗎?
06/04 15:10, 6F

06/04 16:05, , 7F
謝謝 我找到問題了 是我沒加<document>的標籤
06/04 16:05, 7F

06/04 16:06, , 8F
請問<document>是一定要加的嗎 我看網路上的格式都沒
06/04 16:06, 8F

06/04 19:27, , 9F
因為你的 xml 格式錯誤
06/04 19:27, 9F

06/04 19:27, , 10F
xml 有一個 root element,而且是唯一的一個
06/04 19:27, 10F

06/04 19:28, , 11F
像 xhtml 的 root 就是 <html>,root 沒有任何兄弟
06/04 19:28, 11F

06/04 20:22, , 12F
了解了 所以不一定要叫document只是要有一個root就好
06/04 20:22, 12F

06/04 20:22, , 13F
感恩!
06/04 20:22, 13F
文章代碼(AID): #1Foxu8Xf (Ajax)
文章代碼(AID): #1Foxu8Xf (Ajax)