[Javascript]用于发送异步请求的类
/// <summary>
/// 用于发送异步请求
/// </summary>
/// <param name="sUrl">发送请求的URL</param>
/// <param name="sMethod">请求方式"post"/"get"</param>
/// <param name="sPostdata">要发生地Xml字符串</param>
/// <param name="fPostback">回调函数,形如:function(xmldoc){}</param>
function xmlhttp(sUrl,sMethod,sPostdata,fPostback)
{
///静态方法***************************************************
/// <summary>
/// 通过Xml字符串获取XmlDoc对象
/// </summary>
/// <param name="txt">Xml字符串</param>
/// <returns>XmlDoc对象</returns>
xmlhttp.loadXMLString = function(txt)
{
try
{ //Internet Explorer
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async= false;
xmlDoc.loadXML(txt);
return(xmlDoc);
}
catch(e)
{
try
{//Firefox,Mozilla,Opera,etc
parser = new DomParser();
xmlDoc = parser.parseFormString(txt,"text/xml");
return(xmlDoc);
}
catch(e)
{
alert(e.message())
return(null);
}
}
}
/// <summary>
/// xmlHttp.onreadystatechange 事件处理函数
/// </summary>
/// <param name="oxmlhttp">指定一个xmlhttp对象用以外部调用</param>
xmlhttp.Postback = function(oxmlhttp)
{
return function()
{
if(oxmlhttp.readyState==4)
{
if(oxmlhttp.status==200)
fPostback(oxmlhttp.responseXML);
else
alert("error:" + oxmlhttp.status +" "+ oxmlhttp.statusText);
}
}
}
///成员***************************************************
///xmlhttp对象;
this.oXmlHttp = null;
///xmlhttp对象初始化
if(window.ActiveXObject) //如果是IE浏览器
this.oXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");//用ActiveXObject来创建xmlHttp对象
else if (window.XMLHttpRequest) //如果不是IE浏览器
this.oXmlHttp = new XMLHttpRequest(); //用XMLHttpRequest来创建对象
this.oXmlHttp.open(sMethod,sUrl,true); //以异步方式打开远程网页
this.oXmlHttp.setRequestHeader("CONTENT-TYPE","application/x-www-form-urlencoded");//设置HTTP头
this.oXmlHttp.onreadystatechange = xmlhttp.Postback(this.oXmlHttp);
///发送方法
this.fSend = function()
{
this.oXmlHttp.send(xmlhttp.loadXMLString(sPostdata));
}
}
/// 用于发送异步请求
/// </summary>
/// <param name="sUrl">发送请求的URL</param>
/// <param name="sMethod">请求方式"post"/"get"</param>
/// <param name="sPostdata">要发生地Xml字符串</param>
/// <param name="fPostback">回调函数,形如:function(xmldoc){}</param>
function xmlhttp(sUrl,sMethod,sPostdata,fPostback)
{
///静态方法***************************************************
/// <summary>
/// 通过Xml字符串获取XmlDoc对象
/// </summary>
/// <param name="txt">Xml字符串</param>
/// <returns>XmlDoc对象</returns>
xmlhttp.loadXMLString = function(txt)
{
try
{ //Internet Explorer
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async= false;
xmlDoc.loadXML(txt);
return(xmlDoc);
}
catch(e)
{
try
{//Firefox,Mozilla,Opera,etc
parser = new DomParser();
xmlDoc = parser.parseFormString(txt,"text/xml");
return(xmlDoc);
}
catch(e)
{
alert(e.message())
return(null);
}
}
}
/// <summary>
/// xmlHttp.onreadystatechange 事件处理函数
/// </summary>
/// <param name="oxmlhttp">指定一个xmlhttp对象用以外部调用</param>
xmlhttp.Postback = function(oxmlhttp)
{
return function()
{
if(oxmlhttp.readyState==4)
{
if(oxmlhttp.status==200)
fPostback(oxmlhttp.responseXML);
else
alert("error:" + oxmlhttp.status +" "+ oxmlhttp.statusText);
}
}
}
///成员***************************************************
///xmlhttp对象;
this.oXmlHttp = null;
///xmlhttp对象初始化
if(window.ActiveXObject) //如果是IE浏览器
this.oXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");//用ActiveXObject来创建xmlHttp对象
else if (window.XMLHttpRequest) //如果不是IE浏览器
this.oXmlHttp = new XMLHttpRequest(); //用XMLHttpRequest来创建对象
this.oXmlHttp.open(sMethod,sUrl,true); //以异步方式打开远程网页
this.oXmlHttp.setRequestHeader("CONTENT-TYPE","application/x-www-form-urlencoded");//设置HTTP头
this.oXmlHttp.onreadystatechange = xmlhttp.Postback(this.oXmlHttp);
///发送方法
this.fSend = function()
{
this.oXmlHttp.send(xmlhttp.loadXMLString(sPostdata));
}
}
使用事例:
function fPostback(o)
{
alert(o.getElementsByTagName("status")[0].text);
}
sPostdata="<a>hehe</a>";
Myxmlhttp = new xmlhttp('bin\\functions.aspx?method=s','post',sPostdata,fPostback);
Myxmlhttp.fSend();
obj=Myxmlhttp.oXmlHttp;
{
alert(o.getElementsByTagName("status")[0].text);
}
sPostdata="<a>hehe</a>";
Myxmlhttp = new xmlhttp('bin\\functions.aspx?method=s','post',sPostdata,fPostback);
Myxmlhttp.fSend();
obj=Myxmlhttp.oXmlHttp;