登陆 免费注册 提交网站
站长资讯 >> 站长学院 >> 网络编程
 2008-02-20 19:48浏览: 2666 次

利用Ajax传递Xml文档

客户端

//生成XML文件

function GetAllFormData()

{

var strXML = "\r\n\r\n";

strXML += "bccu"

strXML += "25";

strXML += "\r\n"

return strXML;

}

///向服務器发送XML文档

function Send(Str,URL)

{

var Http = new ActiveXObject("Microsoft.XMLHTTP")

Http.open("POST",URL,false)

Http.send(Str)

return Http.responseText;

}

///获得XML中指定的节的值

function GetXMLNodevalue(strXML,nodeName)

{

var Dom = new ActiveXObject("Microsoft.XMLDOM")

Dom.async=false

Dom.loadXML(strXML)

if(Dom.parseError.errorCode != 0)

{

delete(Dom)

return(false)

}

else

{

var node = Dom.documentElement.selectSingleNode("//"+nodeName);

if(node)

nodevalue = node.text;

delete(Dom)

return(nodevalue);

}

}

function Test()

{

var tmp = Send(GetAllFormData(),"./test.aspx");

var name = GetXMLNodevalue(tmp,"UserName");

var password = GetXMLNodevalue(tmp,"Age");

}

服务器端(test.cs)

System.IO.Stream stream = Request.InputStream

System.Xml.XmlDocument doc = new XmlDocument();

try

{

doc.Load(stream); //加载发送过来的Xml文档

}

catch

{

byte[] buffer = new byte[stream.Length];

stream.Read(buffer,0,buffer.Length);

string strXML = System.Text.UnicodeEncoding.Default.GetString(buffer,0,buffer.Length);

doc.LoadXml(strXML);

}

//将doc处理后输出以便返回到客户端(此处省略)

response.write("")

网站首页 - 关于我们 - 联系我们 - 友情链接 - 帮助中心
Copyright ©2007-2022 www.Aisila.com All Rights Reserved
苏ICP备20008864号-4