利用Ajax传递Xml文档
时间:2007-02-18 来源:PHP爱好者
客户端
<script language="JavaScript">
//生成XML文件
function GetAllFormData()
{
var strXML = "<Client>rn<FormData>rn";
strXML += "<UserName>bccu</UserName>"
strXML += "<Age>25</Age>";
strXML += "</FormData>rn</Client>"
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");
}
</script>
服务器端(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("")
<script language="JavaScript">
//生成XML文件
function GetAllFormData()
{
var strXML = "<Client>rn<FormData>rn";
strXML += "<UserName>bccu</UserName>"
strXML += "<Age>25</Age>";
strXML += "</FormData>rn</Client>"
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");
}
</script>
服务器端(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("")
相关阅读 更多 +