原文地址:找不到了,向作者道歉
function
GetXmlHttp()
... {
//return window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
var xmlRequest = null;
try
...{
xmlRequest = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e1)
...{
try
...{
xmlRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e2)
...{
xmlRequest = null;
}
}
if ( !xmlRequest && typeof XMLHttpRequest != "undefined" )
...{
xmlRequest = new XMLHttpRequest();
}
return xmlRequest;
}
// 测试范例
function CheckPassword(username, password)
... {
var sMethod = "CheckPassword"; // 请求的函数
var para = new parameter(); // 定义参数
para.add("username", username); // 添加参数
para.add("password", password); // 添加参数
var sReuqestParam = getRequest_Soap12(sMethod, para);
return (soapRequest("webservice/wsmain.asmx", sReuqestParam, sMethod) == "true");
}
// -------------------------------------------------------------
// WEBService下Soap请求
// sWebServiceName: web Service名
// sMethod:Web Service请求时调用的函数名称
// -------------------------------------------------------------
function soapRequest(sWebServiceName, sRequestParam, sMethod)
... {
var xmlDom = GetXmlHttp();
if (!xmlDom)
...{
return false;
}
//SOAP1.1
// xmlDom.open("POST", sWebServiceName, false);
// xmlDom.setRequestHeader("Content-Type","text/xml; charset=utf-8");
// xmlDom.setRequestHeader("SOAPAction", "http://tempuri.org/" + sMethod);
//SOAP1.2
xmlDom.open("POST", sWebServiceName, false);
xmlDom.setRequestHeader("Content-Type","application/soap+xml; charset=utf-8");
xmlDom.send(sRequestParam);
var sResponse = xmlDom.responseText;
var sResult = getWebServiceResponseValue(sResponse, sMethod)
return sResult;
}
// ------------------------------------------------------------------
// 取得Web Service 返回值
// sResponse:Web Service返回数据(字符串)
// sMethod:Web Service请求时调用的函数名称
// 返回Web Service的结果集,可以是任意类型的数据,如XML、JSON等
// 注意:这个方法有漏洞
// ------------------------------------------------------------------
function getWebServiceResponseValue(sResponse, sMethod)
... {
var pos1 = sResponse.indexOf("<" + sMethod + "Result>");
var pos2 = sResponse.indexOf("</" + sMethod + "Result>");
var index = pos1 + sMethod.length + 2 + 6;
var result = sResponse.substr(index,pos2 - index);
return result;
}
// ------------------------------------------------------
// 生成SOAP1.2请求参数串
// sMethod: 调用的web Service方法
// Para:parameter类型参数,用于传送WEBService方法下的参数列表
// 返回值:SOAP1.2的Web Service请求串
// -----------------------------------------------------
function getRequest_Soap12(sMethod, para)
... {
var sRequest = "<?xml version="1.0" encoding="utf-8"?> ";
sRequest += "<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ";
sRequest += "xmlns:xsd="http://www.w3.org/2001/XMLSchema" ";
sRequest += "xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> ";
sRequest += "<soap12:Body> ";
sRequest += "<" + sMethod + " xmlns="http://tempuri.org/"> ";
sRequest += para.toXml() + " ";
sRequest += "</" + sMethod + "> ";
sRequest += "</soap12:Body> ";
sRequest += "</soap12:Envelope>";
return sRequest;
}
// -----------------------------------------------------
// 生成SOAP1.1请求参数串
// sMethod: 调用的web Service方法
// Para:parameter类型参数,用于传送WEBService方法下的参数列表
// 返回值:SOAP1.1的Web Service请求串
// -----------------------------------------------------
function getRequest_Soap11(sMethod, para)
... {
var sRequest = "<?xml version="1.0" encoding="utf-8"?>";
sRequest += "<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ";
sRequest += "xmlns:xsd="http://www.w3.org/2001/XMLSchema" ";
sRequest += "xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">" + " ";
sRequest += "<soap:Body>";
sRequest += "<" + sMethod + " xmlns="http://tempuri.org/"> ";
sRequest += para.toXml() + " ";
sRequest += "</" + sMethod + "> ";
sRequest += "</soap:Body> ";
sRequest += "</soap:Envelope>";
return sRequest;
}
// -----------------------------------------
// add方法:用于添加参数
// toXML方法:将POST参数转化为XML格式字符串
// toPost方法:将POST参数转化为Post格式字符串
// -----------------------------------------
var parameter = function ()
... {
var parameterList = new Array();
// 添加POST参数
this.add = function(name,value)
...{
parameterList[name] = value;
return this;
}
// 将POST参数转换成XML格式字符串(<param1>value1</param1> <param2>value2</param2> ...)
this.toXml = function()
...{
var xml = "";
for(var p in parameterList)
...{
if(typeof(parameterList[p]) != "function")
xml += "<" + p + ">" + parameterList[p].toString().replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">") + "</" + p + ">";
}
return xml;
}
// 将POST参数转换成POST格式字符串(Param1=Value1&Param2=Value2&...)
this.toPost = function()
...{
var postStr = "";
for (var p in parameterList)
...{
if (typeof(parameterList[p]) != "function")
postStr += p + "=" + parameterList[p] + "&";
}
postStr = postStr.substr(0,postStr.length - 1);
return postStr;
}
}
... {
//return window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
var xmlRequest = null;
try
...{
xmlRequest = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e1)
...{
try
...{
xmlRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e2)
...{
xmlRequest = null;
}
}
if ( !xmlRequest && typeof XMLHttpRequest != "undefined" )
...{
xmlRequest = new XMLHttpRequest();
}
return xmlRequest;
}
// 测试范例
function CheckPassword(username, password)
... {
var sMethod = "CheckPassword"; // 请求的函数
var para = new parameter(); // 定义参数
para.add("username", username); // 添加参数
para.add("password", password); // 添加参数
var sReuqestParam = getRequest_Soap12(sMethod, para);
return (soapRequest("webservice/wsmain.asmx", sReuqestParam, sMethod) == "true");
}
// -------------------------------------------------------------
// WEBService下Soap请求
// sWebServiceName: web Service名
// sMethod:Web Service请求时调用的函数名称
// -------------------------------------------------------------
function soapRequest(sWebServiceName, sRequestParam, sMethod)
... {
var xmlDom = GetXmlHttp();
if (!xmlDom)
...{
return false;
}
//SOAP1.1
// xmlDom.open("POST", sWebServiceName, false);
// xmlDom.setRequestHeader("Content-Type","text/xml; charset=utf-8");
// xmlDom.setRequestHeader("SOAPAction", "http://tempuri.org/" + sMethod);
//SOAP1.2
xmlDom.open("POST", sWebServiceName, false);
xmlDom.setRequestHeader("Content-Type","application/soap+xml; charset=utf-8");
xmlDom.send(sRequestParam);
var sResponse = xmlDom.responseText;
var sResult = getWebServiceResponseValue(sResponse, sMethod)
return sResult;
}
// ------------------------------------------------------------------
// 取得Web Service 返回值
// sResponse:Web Service返回数据(字符串)
// sMethod:Web Service请求时调用的函数名称
// 返回Web Service的结果集,可以是任意类型的数据,如XML、JSON等
// 注意:这个方法有漏洞
// ------------------------------------------------------------------
function getWebServiceResponseValue(sResponse, sMethod)
... {
var pos1 = sResponse.indexOf("<" + sMethod + "Result>");
var pos2 = sResponse.indexOf("</" + sMethod + "Result>");
var index = pos1 + sMethod.length + 2 + 6;
var result = sResponse.substr(index,pos2 - index);
return result;
}
// ------------------------------------------------------
// 生成SOAP1.2请求参数串
// sMethod: 调用的web Service方法
// Para:parameter类型参数,用于传送WEBService方法下的参数列表
// 返回值:SOAP1.2的Web Service请求串
// -----------------------------------------------------
function getRequest_Soap12(sMethod, para)
... {
var sRequest = "<?xml version="1.0" encoding="utf-8"?> ";
sRequest += "<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ";
sRequest += "xmlns:xsd="http://www.w3.org/2001/XMLSchema" ";
sRequest += "xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> ";
sRequest += "<soap12:Body> ";
sRequest += "<" + sMethod + " xmlns="http://tempuri.org/"> ";
sRequest += para.toXml() + " ";
sRequest += "</" + sMethod + "> ";
sRequest += "</soap12:Body> ";
sRequest += "</soap12:Envelope>";
return sRequest;
}
// -----------------------------------------------------
// 生成SOAP1.1请求参数串
// sMethod: 调用的web Service方法
// Para:parameter类型参数,用于传送WEBService方法下的参数列表
// 返回值:SOAP1.1的Web Service请求串
// -----------------------------------------------------
function getRequest_Soap11(sMethod, para)
... {
var sRequest = "<?xml version="1.0" encoding="utf-8"?>";
sRequest += "<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ";
sRequest += "xmlns:xsd="http://www.w3.org/2001/XMLSchema" ";
sRequest += "xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">" + " ";
sRequest += "<soap:Body>";
sRequest += "<" + sMethod + " xmlns="http://tempuri.org/"> ";
sRequest += para.toXml() + " ";
sRequest += "</" + sMethod + "> ";
sRequest += "</soap:Body> ";
sRequest += "</soap:Envelope>";
return sRequest;
}
// -----------------------------------------
// add方法:用于添加参数
// toXML方法:将POST参数转化为XML格式字符串
// toPost方法:将POST参数转化为Post格式字符串
// -----------------------------------------
var parameter = function ()
... {
var parameterList = new Array();
// 添加POST参数
this.add = function(name,value)
...{
parameterList[name] = value;
return this;
}
// 将POST参数转换成XML格式字符串(<param1>value1</param1> <param2>value2</param2> ...)
this.toXml = function()
...{
var xml = "";
for(var p in parameterList)
...{
if(typeof(parameterList[p]) != "function")
xml += "<" + p + ">" + parameterList[p].toString().replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">") + "</" + p + ">";
}
return xml;
}
// 将POST参数转换成POST格式字符串(Param1=Value1&Param2=Value2&...)
this.toPost = function()
...{
var postStr = "";
for (var p in parameterList)
...{
if (typeof(parameterList[p]) != "function")
postStr += p + "=" + parameterList[p] + "&";
}
postStr = postStr.substr(0,postStr.length - 1);
return postStr;
}
}