首页 > 其他分享 >一个调用ATL Server WebServices 的AJAX客户端

一个调用ATL Server WebServices 的AJAX客户端

时间:2023-01-27 14:09:11浏览次数:69  
标签:function ... return ATL Server AJAX params ._ SOAPClient


请将下面的代码完整复制到soapclient.js中

/**/
/*****************************************************************************
AJAX
Javascript "SOAP Client" library
Thank to Mr.Matteo Casati, Ihar Voitka - http://www.guru4.net/
Author: ZhangLiang, E-Mail:[email protected]
Date: 2006-8-31
*****************************************************************************/

function
_debug(rownum, msg)
...
{ alert(rownum+":"+msg); }


var
SOAP_SUCCESS
=

0
;
var
SOAP_FAULT
=

1
;

var
_sr_env_hdr_
=

"
<soap:Envelope
"

+


"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
"

+


"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
"

+


"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
"

+


"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
"
;

var
_sr_body_hdr_
=

"
<soap:Body soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
"
;

var
_soap_header_m_token_
=

""
;
//
global varible, soap_header: m_token


function
SOAPParams(callid, url, method, async, callback, username, password)
...
{
this.callid = callid;
this.url = url; // "http://localhost/MtkWebgisServer/MtkWebgis.dll?Handler=GenMtkWebgisWSDL";
this.svc = null; //"http://cl2/MtkWebgisServer/MtkWebgis.dll?Handler=Default"; // 未完
this.method = method;

if (async+"" == "undefined")
this.async = true; // default value
else
this.async = async;

if (this.async)...{
if (callback+"" == "undefined")
this.callback = soap_callback;
else
this.callback = callback;
}

this.username = null;
if (username+"" != "")
this.username = username;

this.password = null;
if (password+"" != "")
this.password = password;

this.wsdl = null;
this.result = SOAP_FAULT;
this.xmlhttp = null; // xmlhttp object returned

this._pl = new Array(); // input parameters list

this.add = function(name, value)...{
this._pl[name] = value;
return this;
}

this.toXml = function()...{
var xml = "";
for(var p in this._pl)...{
var t = typeof(this._pl[p]);
if (t == "object" || t == "function")
xml += "<" + p + ">" + this._pl[p].toXml() + "</" + p + ">";
else
xml += "<" + p + ">" + this._pl[p].toString().replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">") + "</" + p + ">";
}
return xml;
}

this.setToken = function()...{
if (this.method=="login")...{
_soap_header_m_token_ = this.getval("snp:m_token");
}
}

this.getToken = function()...{
return _soap_header_m_token_;
}

this.getval = function(name)...{
var ret = SOAPClient._getElementsByTagName(this.xmlhttp.responseXML.documentElement, name).item(0);
return ret.text;
}

this.retval = function()...{
return this.getval("return");
}

// SOAP:Fault
this.faultcode = function()...{
return this.getval("faultcode");
}

this.faultstring = function()...{
return this.getval("faultstring");
}

this.faultdetail = function()...{
return this.getval("detail");
}
}


function
SOAPClient()
...
{}


SOAPClient.invoke
=

function
(params)
...
{
params.result = SOAP_FAULT;
if(params.async)
SOAPClient._loadWsdl(params);
else
return SOAPClient._loadWsdl(params);
}


//
private: wsdl cache

SOAPClient_cacheWsdl
=

new
Array();

//
private: invoke async

SOAPClient._loadWsdl
=

function
(params)
...
{
// load from cache?
params.wsdl = SOAPClient_cacheWsdl[params.url];
if(params.wsdl + "" != "" && params.wsdl + "" != "undefined")...{
params.svc = SOAPClient._getService(params.wsdl);
return SOAPClient._sendSoapRequest(params);
}

// get wsdl
var _xmlhttp = SOAPClient._getXmlHttp();

if (params.username+"" != "undefined")
_xmlhttp.open("GET", params.url, params.async, params.username, params.password);
else
_xmlhttp.open("GET", params.url, params.async);

if(params.async)...{
_xmlhttp.onreadystatechange = function()...{
if(_xmlhttp.readyState == 4)
SOAPClient._onLoadWsdl(params, _xmlhttp);
}
}

_xmlhttp.send(null);
if (!params.async)
return SOAPClient._onLoadWsdl(params, _xmlhttp);
}


SOAPClient._onLoadWsdl
=

function
(params, req)
...
{
if (req.readyState == 4 && req.status == 200)...{
params.wsdl = req.responseXML;
params.svc = SOAPClient._getService(params.wsdl);
SOAPClient_cacheWsdl[params.url] = params.wsdl; // save a copy in cache
return SOAPClient._sendSoapRequest(params);
}
return SOAP_FAULT;
}


SOAPClient._sendSoapRequest
=

function
(params)
...
{
// get namespace, ok
var ns = SOAPClient._getAttribValue(params.wsdl.documentElement, "targetNamespace");

// 构造SOAP请求, SOAP 2.0
var sr = _sr_env_hdr_ + "<soap:Header><snp:m_token xmlns:snp=""+ns+"">" +
_soap_header_m_token_ + "</snp:m_token></soap:Header>" +
_sr_body_hdr_ + "<snp:" + params.method + " xmlns:snp="" + ns + "">" +
params.toXml() + "</snp:" + params.method + "></soap:Body></soap:Envelope>";

// send request
params.xmlhttp = SOAPClient._getXmlHttp();
params.xmlhttp.open("POST", params.svc, params.async);
params.xmlhttp.setRequestHeader("SOAPAction", ""#"+params.method+"""); // SOAPAction: "#HelloWorld"
params.xmlhttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");

if(params.async)...{
params.xmlhttp.onreadystatechange = function()...{
if(params.xmlhttp.readyState == 4)
SOAPClient._onSendSoapRequest(params);
}
}

try...{
params.xmlhttp.send(sr);
}
catch(ex)...{}

if (!params.async)
return SOAPClient._onSendSoapRequest(params);
}


SOAPClient._onSendSoapRequest
=

function
(params)
...
{
if (params.xmlhttp.readyState == 4 && params.xmlhttp.status == 200)...{
params.result = SOAP_SUCCESS; // 未完...
params.setToken(); // 设置全局变量: _soap_header_m_token_
}

if(params.async && params.callback)
params.callback(params.result, params);

if (!params.async)
return params.result;
}


//
private: utils

SOAPClient._getElementsByTagName
=

function
(document, tagName, ns)
...
{
try...{
// trying to get node omitting any namespaces (latest versions of MSXML.XMLDocument)
var lst = document.selectNodes(".//*[local-name()=""+ tagName +""]");
if (lst.length == 0)
throw 0;
return lst;
}
catch (ex)...{}

// old XML parser support
if (ns+"" == "undefined")
return document.getElementsByTagName(tagName);

return document.getElementsByTagName(ns+":"+tagName);
}


SOAPClient._getService
=

function
(wsdl)
...
{
var soap_address = SOAPClient._getElementsByTagName(wsdl.documentElement, "address", "soap").item(0);
return SOAPClient._getAttribValue(soap_address, "location");
}


SOAPClient._getAttribValue
=

function
(node, name)
...
{
return (node.attributes[name]+"" == "undefined") ? node.attributes.getNamedItem(name).nodeValue : node.attributes[name].value;
}


//
private: xmlhttp factory

SOAPClient._getXmlHttp
=

function
()
...
{
try...{
if(window.XMLHttpRequest)...{
var req = new XMLHttpRequest();
// some versions of Moz do not support the readyState property and the onreadystate event so we patch it!
if(req.readyState == null)
...{
req.readyState = 1;
req.addEventListener("load",
function()
...{
req.readyState = 4;
if(typeof req.onreadystatechange == "function")
req.onreadystatechange();
},
false);
}
return req;
}
else if(window.ActiveXObject) ...{
return new ActiveXObject(SOAPClient._getXmlHttpProgID());
}
}
catch (ex) ...{}

throw new Error("Your browser does not support XmlHttp objects");
}


SOAPClient._getXmlHttpProgID
=

function
()
...
{
if(SOAPClient._getXmlHttpProgID.progid)
return SOAPClient._getXmlHttpProgID.progid;
var progids = ["Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"];
var o;
for(var i = 0; i < progids.length; i++)
...{
try
...{
o = new ActiveXObject(progids[i]);
return SOAPClient._getXmlHttpProgID.progid = progids[i];
}
catch (ex) ...{};
}
throw new Error("Could not find an installed XML parser");
}
假设ATL XML WebServices实现方法如下:


[id( 1
), helpstring(
"
登入系统, 获得用户令牌和全图URL, 以后任何方法调用必须给出令牌
"
)]

HRESULT login([ in
] BSTR Username, [
in
] BSTR Password);

id( 2
), helpstring(
"
刷新地图, 取地图imgURL
"
)]

HRESULT refresh([out] DOUBLE *
Scale, [out, retval] BSTR
*
imgURL);

客户端IE中调用如下:


< HTML >

< HEAD
>

< TITLE
>
WebGis Demo Page
-
WebGis AJAX Client
</
TITLE
>

< script language
=
"
javascript
"
src
=
"
ajax/soapclient.js
"
></
script
>

< script language
=
"
javascript
"
>

// WebService URL


var
g_url
=

"
http://cl2/mtkwebgis/MtkWebgis.dll?Handler=GenMtkWebgisWSDL
"
;

function soap_callback(rcode, params)
... {
if (rcode == SOAP_SUCCESS)
...{
switch (params.callid)
...{
case "刷新":
imgMap.src = params.retval(); // retval() 取得[out,retval]参数
alert( "当前比例:" + params.getval("Scale") ); // getval(...) 取得[out]参数
break;
}
}
else
...{
alert(params.callid+":发生错误");
}
}

function Login()
... {
// 同步调用, 获得令牌
var soap = new SOAPParams("登录", g_url, "login", false);
soap.add("Username", "cheung");
soap.add("Password", "mine");
if (SOAP_SUCCESS == SOAPClient.invoke(soap))
refresh();
else
alert("登录失败");
}

function Refresh()
... {
// 异步调用刷新, 默认回调函数soap_callback
var soap = new SOAPParams("刷新", g_url, "refresh");
SOAPClient.invoke(soap);
}
</ script
>

</ HEAD
>

< BODY onl oad
=
"
Login();
"
>

< div id
=
"
divMap
"
><
img id
=
"
imgMap
"
></
div
>

</ BODY
>

</
HTML
>

使用soapclient.js需要注意,我假设XML WebServices必须存在如上面所示的login方法,用于验证用户,而且m_token必须是soap头。login原型如下:



一个调用ATL Server WebServices 的AJAX客户端_token

[ soap_method ]
[ soap_header( value = "
m_token
"
, out
=

true
) ]
HRESULT login( /**/ /*[in]*/ BSTR Username,
/**/
/*[in]*/
BSTR Password)
...
{
CHAR szID[MAX_TOKEN_LEN + 1];
*szID = 0;

DWORD dwSize = MAX_TOKEN_LEN;
HRESULT hr = m_spSessionSvc->CreateNewSession(szID, &dwSize, &m_spSession);
CHECK_FAILED(hr);

szID[dwSize] = 0;
m_token = CComBSTR(szID).Detach();

return hr;
}

这样,用户就可以通过soapclient.js在IE中直接调用ATL Server写的XML WebServices了。

(本文写于2006年9月,cheungmine,上海新区时空信息技术有限公司)

 

标签:function,...,return,ATL,Server,AJAX,params,._,SOAPClient
From: https://blog.51cto.com/mapaware/6024044

相关文章

  • SQL Server decimal 和 numeric 区别
    最近看到了decimal和numeric ,又记不起来区别是什么,还是总结一下。decimal和numeric在SQL标准中可以说是等价的的,在SQLServer中是一样的(参考:​​decimal和numeri......
  • SQL Server Powershell 开源数据库管理工具 dbatools
    在Windows中开发自动化运维,除了python就是powershell了,powershell与windows相关产品关联紧密,Windows环境下的自动化开发一般使用powershell,sqlserver亦是如此......
  • SQL Server 增量数据同步
    今天就说说 SQLServer增量数据同步。当公司的业务数据量越来越多的时候,数据分析部门总想用来做报表,或者提炼出有用的运营数据。通常,相关负责人希望将各个业务系统、各种......
  • Windows 2008 双机群集配置(for SQLServer)
    此处配置Windows2008群集,将用于SQLserver2008双机故障转移群集,此处SQLserver的群集基于Windows群集实现。Windows2008集群更容易实现了,只要规划好IP和磁盘分配,可一直......
  • SQLServer 2014 内存优化表
    内存优化表是SQLServer2014的新功能,它是可以将表放在内存中,这会明显提升DML性能。关于内存优化表,更多可参考两位大侠的文章:​​SQLServer2014新特性探秘(1)-内存数据库......
  • Serverless应用优化与注意事项
    通过冷启动优化、对无状态性的认识、Serverless架构下的资源评估、开发者工具的加持等方面的介绍对Serverless架构下的应用优化与注意事项进行总结。函数基础与资源编排1......
  • bash: redis-server: 未找到命令...
    2023-01-27今天使用xshell连接linux中的redis数据库时,出现的该问题“ bash:redis-server:未找到命令...”使用redis-server/root/myredis/redis.conf命令,连接数......
  • 以SQL SERVER和C#聊聊微软生态趋势中产品跨生态组合
    众所周知,老牌大厂微软intel都在裁人,国内短周期的互联网大厂(菊厂,狗厂,福报厂,开水团,鹅厂)更是如此。拿微软来说很多基础软件产品,SQLSERVER,C#开发语言,F#开发语言,Access,POWERBI......
  • ASP.NET Core+Element+SQL Server开发校园图书管理系统(二)
    随着技术的进步,跨平台开发已经成为了标配,在此大背景下,ASP.NETCore也应运而生。本文主要基于ASP.NETCore+Element+SqlServer开发一个校园图书管理系统为例,简述基于MVC三......
  • m扩频通信系统在瑞利信道中的误码率性能matlab仿真
    1.算法描述   本课题,我们主要涉及到两个理论要点,第一个是瑞利衰落条件,第二个是扩频通信。下面分别对这两个理论进行介绍:        第一个是瑞利衰落条件:  ......