使用xmlHttp.responseXml得不到对象,怎么回事?
使用的IE6.0
if(xmlHttp.readyState == 4) {
if(xmlHttp.status == 200) {
var xmlDoc = xmlHttp.responseXml;
alert("xmlDoc is "+xmlDoc);
}
}
提示框内容是:xmlDoc is
问题点数:20、回复次数:19Top
1 楼blackcourser(潜心修炼)回复于 2006-04-24 10:37:03 得分 0
请求调用的代码是:
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.open("GET","list.xml",true);
xmlHttp.send(null);
list.xml文件与代码程序在同一个目录下Top
2 楼xuzuning(唠叨)回复于 2006-04-24 11:24:08 得分 0
1、确认返回的是正确的xml文档
2、确认服务器端在发送xml文档前发送了正确的mime头
如果没有发送正确的mime头或xml文档有错误,将不会产生domxml对象
另外在检查时应用
alert("xmlDoc is "+xmlDoc.xml);Top
3 楼blackcourser(潜心修炼)回复于 2006-04-24 12:43:40 得分 0
xml文档正确,如何确认mime头?Top
4 楼ycs0501(IT'BUG)回复于 2006-04-24 21:06:34 得分 0
关注Top
5 楼yixiaook(沙虚)回复于 2006-04-25 09:08:20 得分 0
先用网页访问一下xml文件,看是否是树型结构,如果是,用responseText看能不能取出内容,如果能取出来,可能是你xml文件中有大写字母,记得xml标记不能大写,我也遇见过这类问题,刚解决。Top
6 楼blackcourser(潜心修炼)回复于 2006-04-25 09:58:35 得分 0
用IE浏览xml文件正常,没有大写
使用responseText 就成了undefined的对象Top
7 楼J2oEE(双子)回复于 2006-04-25 11:39:54 得分 0
text/xmlTop
8 楼sugarsupper(智慧之刃)回复于 2006-04-25 18:09:50 得分 0
晕,是responseXML吧,还有设置content-type为text/xml,指定好encodingTop
9 楼iuhxq(小灰)回复于 2006-04-26 12:20:52 得分 0
不要在自己写代码了,别人都写好了,我们调用吧.
http://xkr.us/code/javascript/XHConn/XHConn.js
UsageXHConn is a data object with a single method. In order to use the object, you must first instantiate a copy of it.
var myConn = new XHConn();
After instantiating the object, it is good practice to check for success and act accordingly.
if (!myConn) // XMLHTTP not available. exit method/handle error.
After you know you have a valid connection to work with, you need to define a function that will fire after your connection is complete. Your function should take a single argument which will be the XMLHttpRequest object with its members intact. [see: XMLHTTPRequest members]
var fnWhenDone = function (oXML) { alert(oXML.responseText); };
Then the final step is to make your actual connection using the connect member function of the XHConn object. The method takes the following arguments:
sURL - String - The string that specifies the path to the resource to connect to. (Note: Cross-domain scripting is explicitly denied with XMLHTTP for security reasons. You may only access files within the same domain as the host file.)
sMethod - String - One of either "GET" or "POST", which specify the type of HTTP request to make.
sVars - String - A string of arguments, encoded in the form var1=val1&var2=val2&.... These are the variables that will be submitted along with the request.
fnDone - Function - A reference to a function that will be called upon completion of the HTTP request. A single argument, the XMLHTTPRequest object, will be passed to the function.
So a typical function call would look something like this:
myConn.connect("mypage.php", "POST", "foo=bar&baz=qux", fnWhenDone);
At this point your connection will be made and the results will be sent to your function. The two major members of the XMLHTTPRequest object that you will deal with are responseText and responseXML.
Top
10 楼blackcourser(潜心修炼)回复于 2006-05-02 19:45:54 得分 0
把jsp的content-type设置为text/xml,去取xml文件还上取不到?Top
11 楼longbill(Longbill)回复于 2006-05-04 16:42:00 得分 0
注意是不是xml文件的语法有问题,你直接使用ie打开那个xml文件,看能不能打开Top
12 楼jiangjohy(晃晃悠悠~~~)回复于 2006-05-05 20:20:07 得分 0
var xmlDoc = xmlHttp.responseXml;
改成:var xmlDoc = xmlHttp.responseXML;Top
13 楼moray()回复于 2006-06-14 16:53:26 得分 0
我的碰到了跟楼主一样的问题,请问楼主解决了吗?能说说是什么原因不?大家说得这些我都检查过了,没有问题,可是还是获取不了。Top
14 楼devek(振侠)回复于 2006-07-07 16:46:20 得分 0
xmlHttp.status == 200
这里改成xmlHttp.status == 0
200是服务器的响应代码 0才是你的本机响应代码Top
15 楼jawsy(风中绝响)回复于 2006-07-11 11:20:01 得分 0
如果你的页面文件没有放在server下面,那xmlHttp.status==0
我是写成jsp,放在tomcat下面,xmlHttp.status==200
要想正确运行代码,需要放置到相应的服务器下面,呵呵Top
16 楼yesur(夜隼)回复于 2006-07-11 16:42:13 得分 0
推荐使用Prototype。jsTop
17 楼lxnk1982(lxnk)回复于 2006-07-23 11:34:39 得分 0
恩,我也是碰到这个问题,读取state.xml不成功。把responseXML换为
requestText能正常显示,responseXML和requestText的content-length 也一样~
更为奇怪的是我把state.xml的内容复制到一个新的XML文件(xml1.xml)中,这两
个XML文件除了文件名不同,其余的都相同。
结果读取xml1.xml正常,state.xml却怎么也读不出来Top
18 楼Taciterry(面朝大海,春暖花开)回复于 2006-07-23 12:12:38 得分 0
asp方面一定要加<%Response.contentType="text/xml"%>Top
19 楼cpwhwq()回复于 2006-07-25 10:41:57 得分 0
可以这样试一下:
1.用浏览器打开XML文件,看是否正确
2.如果正确,将XML文件用UTF-8编码保存
Top




