本文主要是介绍Ajax之二: XMLHttpRequest之POST传递XML (应用环境: Windows + IE + .NET[C#]),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Web.Config 设置 <globalization requestEncoding= "UTF-8 " responseEncoding= "UTF-8 " />
可以支持繁简体中文同时显示
--- HTML表单 ----
<form id= "actitem " name= "actitemedit ">
合同号 <input type= "text " id= "cno ">
项目 <input type= "text " id= "item ">
SID <input type= "text " id= "actid " Width= "72px ">
序号 <input type= "text " id= "sno ">
描述 <input type= "text " id= "desc ">
数量 <input type= "text " id= "qty ">
<a href= "javascript:SaveRec() "> 保存 </a>
</form>
采用XML可以一次传送/接收多条记录
前台传送,先组合XML语句,要符合XML语法.
-----javascript-------
var xhr=null;
function SaveRec()
{
var ssid=document.actitem.actid.value;
var cno=document.actitem.cno.value;
var sno=document.actitem.sno.value;
var item=document.actitem.item.value;
var desc=document.actitem.desc.value;
var qty=document.actitem.qty.value;
//组合一条记录,也可组合多条记录,但必须符合XML语法
var XmlStr= " <?xml version= '1.0 ' encoding= 'UTF-8 '?> "
+ " <REC> "
+ " <CNO> " + cno + " </CNO> "
+ " <SNO> " + sno + " </SNO> "
+ " <ITEM> " + item + " </ITEM> "
+ " <DESC> " + desc + " </DESC> "
+ " <QTY> " + qty + " </QTY> "
+ " <RNDNUM> " + Math.random() + " </RNDNUM> ";
XmlStr=XmlStr+ " <ID> " + ssid + " </ID> <VTYPE> 2 </VTYPE> </REC> ";
CreateXMLHttp(); //每次使用前,都要先创建一个XMLHttpRequest实例
if(xhr==null) return;
xhr.onreadystatechange=GetXMLVal;
xhr.open( "POST ", "aaa.aspx ");
xhr.send(XmlStr);
}
//返回一条XML记录
function GetXMLVal(){
if(xhr.readyState ==4){
if(xhr.status==200){
//直接处理XML
var xmlDoc =xhr.responseXML.documentElement;
var sid=xmlDoc.childNodes[0].childNodes[0].nodeValue;
var sid=xmlDoc.childNodes[1].childNodes[0].nodeValue;
//........
}
}
}
如果返回多条记录:
function GetXMLVal(){
if(xhr.readyState ==4){
if(xhr.status ==200){
var ss=xhr.responseText;
//可用下列语句设置XML Document
var xmlDoc=new ActiveXObject( "Microsoft.XMLDOM ");
xmlDoc.loadXML(ss);
//判断返回的XML字符串是否合法
if(xmlDoc.parseError.errorCode!=0){
var txt= "Return Data Format Error:/r/n/r/n ";
txt=txt+ "Error Code: " + xmlDoc.parseError.errorCode + "/n ";
txt=txt+ "Error Reason: " + xmlDoc.parseError.reason;
txt=txt+ "Error Line: " + xmlDoc.parseError.line;
//txt=txt + "/r/n/r/n " + xhr.responseText;
alert(txt);
return;
}
var x=xmlDoc.documentElement;
var n=x.childNodes.length; //取得记录条数
if(n> 0){
for(var i=0;i <n;i++){ //逐条循环取值
var pid=x.childNodes[i].childNodes[0].childNodes[0].nodeValue;
var sid=x.childNodes[i].childNodes[1].childNodes[0].nodeValue;
var vtxt=x.childNodes[i].childNodes[2].childNodes[0].nodeValue;
.....
}
}
}
}
}
后台表单接收前台所传的XML示例 aaa.aspx.cs
----C#-----
using System.Xml;
private void Page_Load(object sender, System.EventArgs e){
string sid= " ";
string cno= " ";
string sno= " ";
string item= " ";
string desc= " ";
string qty= " ";
XmlDocument XmlDoc;
if(Request.InputStream.Length> 0) {
XmlDoc=new XmlDocument();
try{
XmlDoc.Load(Request.InputStream);
}
catch
{
System.IO.Stream stream = Request.InputStream;
byte[] buffer = new byte[stream.Length];
stream.Read(buffer,0,buffer.Length);
string szXML = System.Text.UnicodeEncoding.Default.GetString(buffer,0,buffer.Length);
XmlDoc.LoadXml(szXML);
}
if(XmlDoc.GetElementsByTagName( "ID ").Count> 0 )
sid=XmlDoc.GetElementsByTagName( "ID ")[0].ChildNodes[0].Value;
if(XmlDoc.GetElementsByTagName( "SNO ").Count> 0)
sno=XmlDoc.GetElementsByTagName( "SNO ")[0].ChildNodes[0].Value;
...其它取值方示相同
}
.....其它处理
//返回结果,重新组合XML返回
String XmlStr= " <?xml version= '1.0 ' encoding= 'UTF-8 '?> <REC> ...同样以UTF-8编码记录内容... </REC> ";
Response.ContentType= "text/xml ";
Response.Write (XmlStr);
Response.End();
}
这篇关于Ajax之二: XMLHttpRequest之POST传递XML (应用环境: Windows + IE + .NET[C#])的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!