ajax post数据至xml的问题(纯新手问题,请高手指点!)

bestfc 2008-08-01 10:26:29
代码如下,提交和显示按钮都出错,请高人指点,我新手,谢谢
<html>
<head><title>Ajax Test</title>
<script language="JavaScript" type="text/javascript">
// global variables to keep track of the request
// and the function to call when done
var ajaxreq=false, ajaxCallback;
// ajaxRequest: Sets up a request
function creatxml()
{
var xml="<?xml version=\"1.0\" ?>";
xml=xml+"<root>";
xml=xml+"<title>"+document.getElementById("title").value+"</title>";;
xml=xml+"<content>"+document.getElementById("content").value+"</content>";
xml=xml+"</root>";
return xml;
}
function ajaxRequest(filename) {
try {
// Firefox / IE7 / Others
ajaxreq= new XMLHttpRequest();
} catch (error) {
try {
// IE 5 / IE 6
ajaxreq = new ActiveXObject("Microsoft.XMLHTTP");
} catch (error) {
return false;
}
}
ajaxreq.open("post","submit2.xml",true);
ajaxreq.onreadystatechange = ajaxResponse;

ajaxreq.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
senddata=creatxml();
ajaxreq.send(senddata);
}
// ajaxResponse: Waits for response and calls a function
function ajaxResponse() {
if (ajaxreq.readyState !=4) return;
if (ajaxreq.status==200) {
// if the request succeeded...
if (ajaxCallback) ajaxCallback();
} else alert("Request failed: " + ajaxreq.statusText);
return true;
}

</script>
</head>
<body>
<h1 style="text-align:center">Ajax Quiz Example</h1>
<div style="text-align:center">
<h3>title</h3><br>
<input type="text" id="title" />
<h3>content</h3><br />
<textarea rows="4" cols="50" id="content"></textarea><br /><br />
<input type="button" value="提交" id="submit1" />
<input type="button" value="显示" id="submit2" />
</div>
<div id="showmsg" style="width:800px;text-align:center;float:left;margin-top:20px">your message will show here</div>
<script language="JavaScript" type="text/javascript">
function showMsg()
{
titles = ajaxreq.responseXML.getElementsByTagName("title"); //xml
contents=ajaxreq.responseXML.getElementsByTagName("content");

msgg="the title is : "+titles[0].firstChild.nodeValue+"<br>"; //xml
msgg+="the content is : "+contents[0].firstChild.nodeValue+"<br>";
document.getElementById("showmsg").firstChild.nodeValue=msgg; //html
}

obj=document.getElementById("submit1");
obj.onclick=ajaxRequest;
obj=document.getElementById("submit2");
obj.onclick=showMsg;
</script>
</body>
</html>
...全文
58 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

52,797

社区成员

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

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