为什么读不出来值?

china2001ok 2006-08-08 10:48:11
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>ajax</title>
</head>

<body>
<input type=text id=obj name=obj value="test"></text>
<button onclick ="ajax('data.xml');">ajax</button>
</body>

</html>
<script>
function ajax(file)
{
var xmlObj=null;
if(window.XMLHttpRequest)
{
xmlObj=new XMLHttpRequest();
}
else if(window.ActiveXObject)
{
xmlObj=new ActiveXObject("Microsoft.XMLHTTP");
}
else
{
return;
}

xmlObj.onreadystatechange=function(){
alert(xmlObj.readyState);
if(xmlObj.readyState==4)
{
obj.value =xmlObj.responseXML.getElementsByTagName("data")[0].firstChild.data;
//processXML("obj",xmlObj.responseXML.getElementsByTagName('data')[0].firstChild.data);
}
}
xmlObj.open('GET',file,true);
xmlObj.send('');

}
function processXML(obj,data)
{
document.getElementById(obj).innerText =data;
}
</script>


data.xml
<?xml version="1.0" encoding="UTF-8"?>
<root>
<data>
this is a test
</data>
</root>


...全文
210 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangjun 2006-08-08
  • 打赏
  • 举报
回复
<script language="javascript" src="prototype.js"></script>
//****************************解析XML
function ajaxXml()
{
var url = "data.xml";
var myAjax = new Ajax.Request
(
url,
{
method: 'Get',
asynchronous:true,
onComplete: showResponseXml
}
);
}
function showResponseXml(originalRequest)
{
//put returned XML in the textarea
if(originalRequest.readyState == 4)
{
if (originalRequest.status == 200)
{
var resp= originalRequest.responseXml;
var data= resp.getElementsByTagName("data");
alert(data[0].firstChild.data);
$("obj").value = data[0].firstChild.data;

}
else
{
alert("error");
}
}
}
china2001ok 2006-08-08
  • 打赏
  • 举报
回复
晕 我用IDEA里用就可以了 这个也要环境的啊
zhangjun 2006-08-08
  • 打赏
  • 举报
回复
没错,我放的就是同一个目录下,data.xml和解析的文件在同一个目录下
china2001ok 2006-08-08
  • 打赏
  • 举报
回复
我就是将这两个文件放在一个目录下 难道还要建立虚拟目录?
zhangjun 2006-08-08
  • 打赏
  • 举报
回复
下载Prototype.js请到http://prototype.conio.net/
zhangjun 2006-08-08
  • 打赏
  • 举报
回复
//****************没有用到Prototype
function ajaxSubmitxml(){
//创建XMLHttpRequest对象
var xmlhttp;
try{
xmlhttp=new XMLHttpRequest();
}catch(e){
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
//创建请求结果处理程序
xmlhttp.onreadystatechange=function(){
if (4==xmlhttp.readyState)
{
if (200==xmlhttp.status)
{
var resp= xmlhttp.responseXml;
var data= resp.getElementsByTagName("data");
alert(data[0].firstChild.data);

}
else
{
alert("error");
}
}
}
//打开连接,true表示异步提交
xmlhttp.open("get", "data.xml", true);
//发送数据
xmlhttp.send();
}

ABCatai 2006-08-08
  • 打赏
  • 举报
回复
<script language="javascript" src="prototype.js"></script>中的prototype.js是一个JavaScript包,这个程序包里面包含了许多预定义的对象和通用性方法。编写这些方法的明显的目的就是为了减少你大量的重复编码和惯用法,当然你不用也可以
ABCatai 2006-08-08
  • 打赏
  • 举报
回复
楼主的代码我复制后运行,"this is a test"打印出来,没问题!!
我改的地方有:
1,
<script language="javascript">
</script>

2,
就是通过ajax(file)中的参数file来传递data.xml地址时,我用的是eclipse,data.xml文件我建立在WebContent下,file = "/BookOfAccountWeb/data.xml"; 其中BookOfAccountWeb是我建的项目名称

其他什么也没改变!!
china2001ok 2006-08-08
  • 打赏
  • 举报
回复
<script language="javascript" src="prototype.js"></script>
这个也要么?

52,797

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Ajax
社区管理员
  • Ajax
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧