Ajax示例代码_一般处理程序输出responseXML
1.新建一般处理处理程序例子:
//指定http输出类型
context.Response.ContentType="text/XML";
Book b=new BookServices().GetBookById(Convert.toInt32(context.request.Params["BookId"]));//根据Id获取book的信息,返回book对象
//定义XML输入流(XmlTextWriter(指定二进制格式输出,指定编码格式))
XmlWriter writer=new XmlTextWriter(context.response.outputstream,Encoding.UTF8);
//定义XmlSerializer 将对象序列化到XML文档
(将指定对象反序列化到Xml)
XmlSerializer seriaizer=new XmlSerializer(b.GetType());
//写入文件到XML序列化流中
(流对象,要写入的对象)
seriaizer.Serialize(writer,b);
//关闭流
writer.close();
=====================================================================================================================================================
2.处理HTML页面
2.1 JavaScript脚本
function btnClick(){
var bookId=document.getElementById("BookId").value;
var url="_Hander/BookList.aspx?BookId="+bookId;
//定义全局xhr
var xhr;
if(window.ActivexObject){
xhr=new ActivexObjext("Microsoft.XMLHTTP");
}else if(window.XMLHttpRequest){
xhr=new XMLHttpRequest();
}
xhr.onreadystatechange=function(){
if(xhr.readyState==4&&xhr.status==200){
var dom=xhr.responseXML;
document.getElementById("Id").innerHTML =
"编号:"+dom.getElementsByTagName("Id").text;
document.getElementById("Title").innerHTML =
"书名:" + dom.getElementsByTagName("Title").text;
document.getElementById("Author").innerHTML =
"作者:" + dom.getElementsByTagName("Author").text;
document.getElementById("PublisherId").innerHTML =
"出版社:" + dom.getElementsByTagName("PublisherId").text;
document.getElementById("PublishDate").innerHTML =
"出版时间:" + dom.getElementsByTagName("PublishDate").text;
document.getElementById("ISBN").innerHTML =
"ISBN:" + dom.getElementsByTagName("ISBN").text;
document.getElementById("WordsCount").innerHTML =
"WordsCount:" + dom.getElementsByTagName("WordsCount").text;
document.getElementById("UnitPrice").innerHTML =
"UnitPrice:" + dom.getElementsByTagName("UnitPrice").text;
document.getElementById("ContentDescription").innerHTML =
"ContentDescription:" + dom.getElementsByTagName("ContentDescription").text;
document.getElementById("AurhorDescription").innerHTML =
"AurhorDescription:" + dom.getElementsByTagName("AurhorDescription").text;
document.getElementById("EditorComment").innerHTML =
"EditorComment:" + dom.getElementsByTagName("EditorComment").text;
document.getElementById("TOC").innerHTML =
"TOC:" + dom.getElementsByTagName("TOC").text;
document.getElementById("cateGorie").innerHTML =
"cateGorie:" + dom.getElementsByTagName("cateGorie").text;
}
}
xhr.open("GET",url,true);
xhr.send(null);
}
}
=============================================================================================================================================================
3 HTML
<body>
<p>
<input id="Text1" type="text" />
<input id="Button1" type="button"
value="button"/></p>
<ul>
<li id="Id"></li>
<li id="Title"></li>
<li id="Author"></li>
<li id="PublisherId"></li>
<li id="PublishDate"></li>
<li id="ISBN"></li>
<li id="WordsCount"></li>
<li id="UnitPrice"></li>
<li id="ContentDescription"></li>
<li id="AurhorDescription"></li>
<li id="EditorComment"></li>
<li id="TOC"></li>
<li id="cateGorie"></li>
</ul>
</body>
谢谢分享!!! 谢谢分享!!!
页:
[1]