js操作Xml(向服务器发送Xml,处理服务器返回的Xml)(IE下有效)
人气:0
前台:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>js操作Xml(向服务器发送Xml,处理服务器返回的Xml)(IE下有效)</title>
<script type="text/javascript"><!--
var xmlHttp = null;//XmlHttp对象,Ajax核心
//创建一个Xml文档,向服务器发送.
function f(){
var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.3.0");//1创建xml对象,Active控件.
xmlDoc.async = false;//设置异步还是非异步
xmlDoc.loadXML("<root><name>tree</name><pwd>pwd</pwd></root>");
sendXml( xmlDoc,'Default.aspx');
}
//向服务器发送Xml文档
function sendXml(xmlDoc,serverURL){
xmlHttp = new ActiveXObject ("Msxml2.XMLHTTP.3.0");//xmlhttp对象,异步传输.
var strDoc;
if (typeof(xmlDoc) == "object")//判断,这里是object
strDoc = xmlDoc.xml;
else
strDoc = xmlDoc;
xmlHttp.open ("POST","Default.aspx" ,true);//第三个参数如果为真,则调用onreadystatechange属性指定的回调函数。
xmlHttp.onreadystatechange=getData;
xmlHttp.send(strDoc);//向服务器发传的数据.
}
function getData(){
if (xmlHttp.readyState==4) //状态为4表示完成.
{
var strxml=xmlHttp.responseText;//取得返回的Xml
alert(strxml);
}
}
// --></script>
</head>
<body>
<input type="button" onclick="f();" value="request" />
</body>
</html>
Ajax Server:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
public partial class testXml_Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(Request.InputStream);//接收到客户端传来的xml
XmlNode rootnode = xmldoc.DocumentElement;
XmlNode pwd = rootnode.SelectSingleNode("pwd");
pwd.InnerText = "changed";//服务器端改变xml文档内容
Response.Write(xmldoc.InnerXml);//返回修改后的Xml文档
Response.End();
}
}
复制代码 代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>js操作Xml(向服务器发送Xml,处理服务器返回的Xml)(IE下有效)</title>
<script type="text/javascript"><!--
var xmlHttp = null;//XmlHttp对象,Ajax核心
//创建一个Xml文档,向服务器发送.
function f(){
var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.3.0");//1创建xml对象,Active控件.
xmlDoc.async = false;//设置异步还是非异步
xmlDoc.loadXML("<root><name>tree</name><pwd>pwd</pwd></root>");
sendXml( xmlDoc,'Default.aspx');
}
//向服务器发送Xml文档
function sendXml(xmlDoc,serverURL){
xmlHttp = new ActiveXObject ("Msxml2.XMLHTTP.3.0");//xmlhttp对象,异步传输.
var strDoc;
if (typeof(xmlDoc) == "object")//判断,这里是object
strDoc = xmlDoc.xml;
else
strDoc = xmlDoc;
xmlHttp.open ("POST","Default.aspx" ,true);//第三个参数如果为真,则调用onreadystatechange属性指定的回调函数。
xmlHttp.onreadystatechange=getData;
xmlHttp.send(strDoc);//向服务器发传的数据.
}
function getData(){
if (xmlHttp.readyState==4) //状态为4表示完成.
{
var strxml=xmlHttp.responseText;//取得返回的Xml
alert(strxml);
}
}
// --></script>
</head>
<body>
<input type="button" onclick="f();" value="request" />
</body>
</html>
Ajax Server:
复制代码 代码如下:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
public partial class testXml_Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(Request.InputStream);//接收到客户端传来的xml
XmlNode rootnode = xmldoc.DocumentElement;
XmlNode pwd = rootnode.SelectSingleNode("pwd");
pwd.InnerText = "changed";//服务器端改变xml文档内容
Response.Write(xmldoc.InnerXml);//返回修改后的Xml文档
Response.End();
}
}
加载全部内容