js ajax 传送xml dom对象到服务器

客户端代码
1
<script> 2 var isie = true; 3 var xmlhttp = null; 4 function createXMLHTTP() {//创建XMLXMLHttpRequest对象 5 if (xmlhttp == null) { 6 if (window.XMLHttpRequest) { 7 xmlhttp = new XMLHttpRequest(); 8 } 9 else { 10 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 11 } 12 //if (xmlhttp == null) { 13 // alert(‘你的浏览器不支持AJAX‘); 14 //} 15 } 16 if ($.browser.msie) {//IE浏览器 17 isie = true; 18 } 19 else { 20 isie = false; 21 } 22 23 } 24 //创建XML文档那个对象,也是第一步 25 function createDomXml() { 26 var domXml = null; 27 //IE浏览器创建xml dom对象 28 var signs = ["Msxml2.DOMDocument.5.0", "Msxml2.DOMDocument.4.0", 29 30 "Msxml2.DOMDocument.3.0", "Msxml2.DOMDocument", "Microsoft.XmlDom"]; 31 for (var i = 0; i < signs.length; i++) { 32 try { 33 domXml = new ActiveXObject(signs[i]); 34 break; 35 } 36 catch (e) { } 37 } 38 39 //其他浏览器 40 if (domXml == null) 41 domXml = document.implementation.createDocument("", "", null); 42 return domXml; 43 } 44 //第二步:客户端获取数据写入xml文档对象里 45 function createXml(dom) { 46 var root = doc.createElement("root");//创建根结点 47 var assign_id = doc.createElement("id");//子节点 48 var assign_ans = doc.createElement("answer");//子节点 49 if (isie) { 50 assign_id.text = "1"; 51 assign_ans.text = "1"; 52 } 53 else { 54 assign_id.textContent = "1"; 55 assign_ans.textContent = "1"; 56 } 57 58 59 root.appendChild(assign_id); 60 61 root.appendChild(assign_ans); 62 63 dom.appendChild(choose); 64 } 65 function submit() { 66 var dom = createDomXml(); 67 createXMLHTTP(); 68 if (xmlhttp == null) { 69 alert("你的浏览器不支持ajax,请换个浏览器试试"); 70 return 0; 71 } 72 createXml(dom); 73 74 xmlhttp.onreadystatechange = success; 75 xmlhttp.open("post", "test.aspx", true);//接收请求页面 76 //xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 77 xmlhttp.send(dom); 78 } 79 80 function success()//异步处理函数,当服务成功返回时 81 { 82 if (xmlhttp.readyState == 4) { 83 alert(xmlhttp.status); 84 if (xmlhttp.status == 200) { 85 alert(xmlhttp.responseText); 86 } 87 else { 88 alert("test error"); 89 } 90 } 91 } 92 </script>
服务器端代码 
1
protected void Page_Load(object sender, EventArgs e) 2 { 3 string res; 4 Request.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8"); 5 XmlDocument xmlDoc = new XmlDocument(); 6 xmlDoc.Load(Request.InputStream); 7 XmlNode node = xmlDoc.SelectSingleNode("choose"); 8 foreach (XmlNode child in node.ChildNodes) 9 { 10 res + child.innerText; 11 } 12 Response.Write(res); 13 Response.End(); 14 } 15 }

 

js ajax 传送xml dom对象到服务器,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。