蓝森林首页 | 返回主页 | 本站地图 | 站内搜索 | 联系信箱 |
 您目前的位置:首页 > 自由软件 > 技术交流 > 应用编程


    

蓝森林 http://www.lslnet.com 2006年8月25日 8:28

[Ajax]--错误--缺少对象

[color=#ff0000]报错--20行缺少对象
小弟初次研究Ajax深受打击,望各位不吝赐教,越深入越好!
[/color]

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="zh" dir="ltr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>使用ajax开发web应用程序 - 示例</title>

<script type="text/javascript"><!--
function ajaxRead(file){
var xmlObj = null;
if(window.XMLHttpRequest){
xmlObj = new XMLHttpRequest();
} else if(window.ActiveXObject){
xmlObj = new ActiveXObject("Microsoft.XMLHTTP");
} else {
return;
}
xmlObj.onreadystatechange = function(){
if(xmlObj.readyState == 4){
[color=#ff0000]//下面是20行,据我分析是firstChild.data处的错误,不知是否正确[/color]
updateObj('xmlObj', xmlObj.responseXML.getElementsByTagName('data')[0].firstChild.data);
}
}
xmlObj.open ('GET', file, true);
xmlObj.send ('');
}
function updateObj(obj, data){
document.getElementById(obj).firstChild.data = data;
}
//--></script>
</head>
<body>
<h1>使用ajax开发web应用程序</h1>
<p>这个页面演示了AJAX技术如何通过动态读取一个远程文件来更新一个网页的内容--不需要任何网页的重新加载。注意:这个例子对于禁止js的用户来说没有效果。</p>
<p id="xmlObj">
这是一些示例数据,它是这个网页的默认数据 <a href="data.xml"
title="查看这个XML数据." onclick="ajaxRead('data.xml'); this.style.display='none'; return false">查看XML数据.</a>
</p>
</body>
</html>

我查MSDN关于responseXML的Remark:
[code]
Remarks
The property is read-only. For security reasons, the parser validation features are always turned off to prevent Microsoft&reg; XML Core Services (MSXML) from attempting to download a document type definition (DTD) or XML-Data definition. If the response entity body is not valid XML, this property returns DOMDocument that was parsed so that you can access the error. This property does not return IXMLDOMParseError itself, but it is accessible from DOMDocument.

If the response was generated by an Active Server Pages (ASP) page and the Multipurpose Internet Mail Extension (MIME) type was not correctly set to "text/xml" using the ASP method Response.ContentType, responseXML will be empty.
[/code]

所以我觉得有可能是你没有取到正确的文件,或者是文件不能解析,或者是文件里没有data节点的第一个子节点,应该在取值前判断一下xmlObj.status,再判断一下data节点是不是存在。试试看先用alert(xmlObj.responseXML.xml)看看能不能取到值


突然发现你updateObj()函数里引用document.getElementById(obj).firstChild.data。可是查MSDN说HTML的DOM里只有comment,object,textnode三个对象有data属性,所以可能执行正常也会看不到结果

谢谢楼上的!
我是对照
http://www.sheneyan.com/html/article/ajax/example.html
[url=http://www.sheneyan.com/html/article/ajax/example.html]http://www.sheneyan.com/html/article/ajax/data.xmll
所写的,刚才我把源码拷贝下来在本地运行,一样的问题,
我疑惑,我的机器2003,ie6难道有,配置问题?望各位解答!!

d

我晕!
我建个网站用,访问http的方式运行,就可以了!
这是为什么???Ajax不是全客户端的技术吗?怎么要这样??
我搞不懂!
请教!!!!

firstChild的确有data属性,没错

你最好输出以下
updateObj('xmlObj', xmlObj.responseXML.getElementsByTagName('data')[0].firstChild.data);
这行的各个参数是否正确传递

现在可以正常运行了!

我的疑问是
     为什么要用http的方式访问才可以正常运行?
     我理解Ajax是客户端的技术,运行直接点击运行(访问本地文件方式)应该可以
     运行的?可实际不是这样的。原因何在?

我来放个马后炮:

xmlHTTP实际上是通过http协议去访问服务器的80端口来获得数据的,你直接在本地运行当然不可以

其实你可以采用xmlDOM的load方法来获取,这是真正的纯客户端,不需要服务器

这样啊!了解, 谢谢!

据说:ff可以在本地运行那个例子...未测试

Sheneyan在上个帖子中说


哈哈,的确啊!试了一下!果然可以!可这又是因为什么呢???

如果"xmlHTTP实际上是通过http协议"那ff有为什么可以本地运行呢??
请高人献身说法!!!!

d

小弟继续提出一个问题

为什么放在纯html的空间里就无法使用这个程序了?
是不是无法在纯html的空间里实现ajax功能了?
是否需要改点什么?
或者有其他的什么变通方式来实现?

我现在已经解决了。。。。。。
因为utf-8没有改成gb2312




Copyright © 1999-2000 LSLNET.COM. All rights reserved. 蓝森林网站 版权所有。 E-mail : webmaster@lslnet.com