首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

应用javascript调用webservice示例

2012-11-23 
使用javascript调用webservice示例再javascript中使用soap调用webservice的示例代码代码再IE6和FF测试通过

使用javascript调用webservice示例

再javascript中使用soap调用webservice的示例代码

代码再IE6和FF测试通过,对于c#写的webservice和java(xfire)写的,都测试过,没有问题

此代码原型来源于 http://www.guru4.net/ 的javascript soapclient

发现这个下载的js只能用于调用c#的webservice,所以利用mootools,重新封装,达到IE和火狐的兼容的同时,兼容java和c#

(再例子中使用的 mootools.v1.11.js 文件,做过修改)

客户端js调用代码如下

js 代码?
  1. function?ajaxRequest() ?? ????{ ??
  2. ????????var?url?=?"http://localhost:88/webservicedemo.asmx"; ?? ???????? ??
  3. ????????//设置webService传入参数 ?? ????????// ??
  4. ????????//注意: ?? ????????// ??
  5. ????????//????调用.Net?写的webservice(如例子中的webservicedemo.asmx) ?? ????????//???????????HelloTo(String?name)???针对name参数必须写成?<name></name>wqj,还有更多参数一样写,使用名称匹配 ??
  6. ????????//???????????传入的参数数量可以不等于(多于或少于)方法要求的参数 ?? ????????// ??
  7. ????????//????调用java(xfire)?发布的webService ?? ????????//???????????传入的参数必须与调用方法的参数数量相等,且按传入值的顺序进行匹配 ??
  8. ????????// ?? ???????? ??
  9. ????????var?para?=?"<name></name>wqj";?这里应该是一个标准的xml形式,源码贴出来时被虑掉了,请参看附件源码?? ???????? ??
  10. ????????var?op?=?{ ?? ????????????????????data:para, ??
  11. ????????????????????onComplete:?showResponse, ?? ????????????????????onFailure:showError, ??
  12. ????????????????????update:'ajaxBack' ?? ?????????????????}; ??
  13. ?? ????????var?service?=?new?WebService(url,"HelloTo",op); ??
  14. ????????service.request(); ?? ????????return?false; ??
  15. ????} ?? ????function?showError(obj) ??
  16. ????{ ?? ????????//obj?是一个xmlHttpRequest对象 ??
  17. ????????alert("error"); ?? ????} ??
  18. ????function?showResponse(requestText,requestXML) ?? ????{ ??
  19. ????????//requestText?返回的文本 ?? ????????//requestXML?返回的XML ??
  20. ????????alert("ok"); ?? ????}??

WebService类的代码如下(webservice.js)

js 代码?
  1. ?? var?WSDLS?=?{}; ??
  2. ?? var?WebService?=?new?Class({ ??
  3. ?? ????url?:?'', ??
  4. ????method?:?'', ?? ????options:? ??
  5. ????{ ?? ????????method:'GET', ??
  6. ????????data:?null, ?? ????????update:?null, ??
  7. ????????onComplete:?Class.empty, ?? ????????onError:Class.empty, ??
  8. ????????evalScripts:?false, ?? ????????evalResponse:?false??
  9. ????}, ?? ???? ??
  10. ????initialize:?function(url,method,options) ?? ????{ ??
  11. ????????this.url?=?url; ?? ????????this.method?=?method; ??
  12. ????????this.options?=?options; ?? ??
  13. ????}, ?? ???? ??
  14. ????request?:?function() ?? ????{ ??
  15. ????????var?wsdl?=?WSDLS[this.url]; ?? ????????if(!wsdl)? ??
  16. ????????{ ?? ????????????var?op?=?{method:'GET',async:?false}; ??
  17. ????????????var?wsdlAjax?=?new?XHR(op).send(this.url?+?"?wsdl",?null);?????????? ?? ????????????wsdl?=?wsdlAjax.transport.responseXML; ??
  18. ????????????WSDLS[this.url]?=?wsdl; ?? ????????} ??
  19. ????????this.setSoap(wsdl); ?? ????}, ??
  20. ???????? ?? ????setSoap?:?function(wsdl) ??
  21. ????{ ?? ???????? ??
  22. ????????var?ns?=?(wsdl.documentElement.attributes["targetNamespace"]?+?""?==?"undefined")???wsdl.documentElement.attributes.getNamedItem("targetNamespace").nodeValue?:?wsdl.documentElement.attributes["targetNamespace"].value; ?? ????????var?sr?=? ??
  23. ????????????????"<!---->"?+ ?? ????????????????""
  24. ????????????????"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"?"?+ ?? ????????????????"xmlns:xsd="http://www.w3.org/2001/XMLSchema"?"?+ ??
  25. ????????????????"xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">"?+ ?? ????????????????"<soap:body>"</soap:body>?+ ??
  26. ????????????????"<"?+?this.method?+?"?xmlns=""?+?ns?+?"">"?+ ?? ?????????????????????(this.options.data?===?null??"":this.options.data)?+ ??
  27. ????????????????"?+?this.method?+?">; ?? ???????? ??
  28. ????????this.options.method?=?'post'; ?? ????????this.options.data?=?null; ??
  29. ???????? ?? ????????var?soapaction?=?((ns.lastIndexOf("/")?!=?ns.length?-?1)???ns?+?"/"?:?ns)?+?this.method; ??
  30. ???????? ?? ????????var?soapAjax?=?new?Ajax(this.url,this.options); ??
  31. ????????soapAjax.setHeader("SOAPAction",?soapaction); ?? ????????soapAjax.setHeader("Content-type",?"text/xml;?charset=utf-8"); ??
  32. ????????soapAjax.request(sr); ?? ????} ??
  33. ???? ?? });??

?在第一个版本中存在以下问题

1. 不能根据webservice的要求输入参数自动组织参数

2. 没有处理返回值

3.一旦webservice调用过程出错,会形成一个死循环(一直弹出error)

?

V2 说明

1. 解决第一版中死循环的问题

2. 统一输入参数的传入形式(与mootools的ajax使用方式完全一致),形式如name=wqj&age=20&........

3. 自动根据参数名对应的值,组织webservice的传入参数,只根据webservice要求的参数名查找对应的值

??? 与顺序不再有关系.(对于xfire中的输入参数使用名称 in0,in1........)

??? 传入的参数数量也不再要求一致,多的自动丢弃,少的自动传空

4. 对于返回的XML,增加提取方法,返回需要的关键返回值(去掉XML的框框)

详细参照附件源码,下面是部分关键代码

?

WebService类的代码如下(webservice.js)

?

js 代码
  1. var?WSDLS?=?{};?? ??
  2. var?WebService?=?new?Class({?? ??
  3. ????url?:?'',?? ????method?:?'',??
  4. ????options:??? ????{??
  5. ????????method:'GET',?? ????????data:?null,??
  6. ????????update:?null,?? ????????onComplete:?Class.empty,??
  7. ????????onError:Class.empty,?? ????????evalScripts:?false,??
  8. ????????evalResponse:?false?? ????},??
  9. ?????? ????initialize:?function(url,method,options)??
  10. ????{????????? ????????this.url?=?url;??
  11. ????????this.method?=?method;?? ????????this.options?=?options;??
  12. ????},?? ??????
  13. ????request?:?function()?? ????{??
  14. ????????var?wsdl?=?WSDLS[this.url];?? ????????if(!wsdl)???
  15. ????????{?? ????????????var?op?=?{method:'GET',async:?false};??
  16. ????????????var?wsdlAjax?=?new?XHR(op).send(this.url?+?"?wsdl",?null);???????????? ????????????wsdl?=?wsdlAjax.transport.responseXML;??
  17. ????????????WSDLS[this.url]?=?wsdl;?? ????????}??
  18. ?? ????????this.setSoap(wsdl);??
  19. ????},?? ??????????
  20. ????setSoap?:?function(wsdl)?? ????{??
  21. ????????var?paraXML?=?this.getParaXML(wsdl);?? ????????alert(paraXML);??
  22. ????????var?ns?=?(wsdl.documentElement.attributes["targetNamespace"]?+?""?==?"undefined")???wsdl.documentElement.attributes.getNamedItem("targetNamespace").nodeValue?:?wsdl.documentElement.attributes["targetNamespace"].value;?? ????????var?sr?=???
  23. ????????????????""?+?? ????????????????"?+??
  24. ????????????????"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"?"?+?? ????????????????"xmlns:xsd="http://www.w3.org/2001/XMLSchema"?"?+??
  25. ????????????????"xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">"?+?? ????????????????"<soap:body>"</soap:body>?+??
  26. ????????????????"<"?+?this.method?+?"?xmlns=""?+?ns?+?"">"?+?? ????????????????????paraXML??+??
  27. ????????????????"<!---->?+?this.method?+?">";?? ??????????
  28. ????????this.options.method?=?'post';?? ????????this.options.data?=?null;??
  29. ?????????? ????????var?soapaction?=?((ns.lastIndexOf("/")?!=?ns.length?-?1)???ns?+?"/"?:?ns)?+?this.method;??
  30. ?? ????????var?soapAjax?=?new?Ajax(this.url,this.options);??
  31. ????????soapAjax.setHeader("SOAPAction",?soapaction);?? ????????soapAjax.setHeader("Content-type",?"text/xml;?charset=utf-8");??
  32. ????????soapAjax.request(sr);??? ????},??
  33. ????getParaXML?:?function(wsdl)?? ????{??
  34. ?????????? ????????var?objNode?=?null;??
  35. ????????var?rtnValue?=?"";?? ????????//java(xfire)??
  36. ????????var?ell?=?this.getElementsByTagName(wsdl,"xsd:element");?????? ????????if(ell.length?==?0)???
  37. ????????{?? ????????????//c#??
  38. ????????????ell?=?this.getElementsByTagName(wsdl,"s:element");???? ????????}??
  39. ????????for(var?i?=?0;?i?<?ell.length;?i++)?? ????????{??
  40. ????????????if(this.getElementAttrValue(ell[i],"name")?==?this.method)?? ????????????{??
  41. ????????????????objNode?=?ell[i];?? ????????????????break;??
  42. ????????????}?? ????????}??
  43. ?? ????????if(objNode?==?null)?return?rtnValue;??
  44. ????????//java(xfire)?? ????????ell?=?this.getElementsByTagName(objNode,"xsd:element");???
  45. ????????if(ell.length?==?0)??? ????????{??
  46. ????????????//c#?? ????????????ell?=?this.getElementsByTagName(objNode,"s:element");??
  47. ????????}?? ????????if(ell.length?==?0)?return?rtnValue?;??
  48. ?????????? ????????var?hash?=?new?Hash();??
  49. ?????????? ????????if(this.options.data?!=?null?&&?this.options.data.clean?!=?"")??
  50. ????????{?? ????????????hash?=?this.options.data.split("&").toHash("=");??
  51. ????????}?? ??????????
  52. ????????for(var?i?=?0;?i?<?ell.length;?i++)?? ????????{??
  53. ????????????var?paraName?=?this.getElementAttrValue(ell[i],"name");?? ????????????rtnValue?=?rtnValue?+?this.getSingleXML(paraName,hash);??
  54. ????????}?? ??????????
  55. ????????return?rtnValue;?? ????},??
  56. ?????? ????getSingleXML?:?function?(name,hash)??
  57. ????{?? ????????name?=?name.trim();??
  58. ?????????? ????????var?rtnValue?=?"";??
  59. ????????if(hash.hasKey(name))?? ????????{??
  60. ????????????rtnValue?=?hash.get(name);?? ????????}??
  61. ????????rtnValue?=?"<"?+?name?+?">"?+?xmlscc(rtnValue)?+?"<!---->?+?name?+?">"?? ????????return?rtnValue;??
  62. ????},?? ????getBackData:?function(xml)??
  63. ????{?? ????????var?rtnValue?=?"";??
  64. ????????//java(xfire)?? ????????var?soap?=?this.getElementsByTagName(xml,"ns1:out");??????
  65. ????????if(soap.length?==?0)?? ????????{??
  66. ????????????//c#?? ????????????soap?=?this.getElementsByTagName(xml,this.method?+?"Result");??
  67. ????????}?? ????????return?soap[0].childNodes[0].nodeValue;???????
  68. ?????????? ????},??
  69. ????getElementsByTagName?:?function(objNode,tagName)?? ????{??
  70. ????????//tagName?形式如?xsd:element?,写出tag的全称?? ??
  71. ????????var?ell;?? ????????if(this.isIE())??
  72. ????????{?? ????????????ell?=?objNode.getElementsByTagName(tagName);??????
  73. ????????}?? ????????else??
  74. ????????{?? ????????????if(tagName.contains(":"))?tagName?=?tagName.split(":")[1];??
  75. ????????????ell?=?objNode.getElementsByTagName(tagName);??????????? ????????}??
  76. ????????return?ell;?? ????},??
  77. ????getElementAttrValue?:?function(objNode,attrName)?? ????{??
  78. ????????var?rtnValue?=?"";?? ??????????
  79. ????????if(objNode?==?null)?return?rtnValue;?? ??????????
  80. ????????if(objNode.attributes[attrName]?+?""?==?"undefined")?? ????????{???
  81. ????????????if(objNode.attributes.getNamedItem(attrName)?!=?null)?? ????????????????rtnValue?=?objNode.attributes.getNamedItem(attrName).nodeValue?;??
  82. ?????????????? ????????}??
  83. ????????else?? ????????{??
  84. ????????????if(objNode.attributes[attrName]?!=?null)?? ????????????????rtnValue?=?objNode.attributes[attrName].value;??
  85. ????????}?? ????????return?rtnValue;??
  86. ????},?? ????isIE?:?function()??
  87. ????{?? ????????var?isMSIE?=?/*@cc_on!@*/false;??
  88. ????????return?isMSIE;?? ????}??
  89. });?? ??
  90. Array.extend({?? ??????
  91. ????toHash?:?function?(splitChar)?? ????{??
  92. ????????var?hash?=?new?Hash({});?? ????????for(var?i=0;i<this.length;i++)??
  93. ????????{?? ??????????????
  94. ????????????if(this[i].split(splitChar).length?==?1)?contrnue;?? ??
  95. ????????????var?key?=?this[i].split(splitChar)[0].trim();?? ????????????var?value?=?this[i].split(splitChar)[1].trim();??
  96. ?????????????? ????????????hash.set(key,?value);??
  97. ????????}?? ??????????
  98. ????????return?hash;?? ????}??
  99. });?? ??
  100. function?xmlscc(strData)?? {??
  101. ?? ????strData=strData.replace(/&/g,?"&");??
  102. ????strData=strData.replace(/>/g,?">");?? ????strData=strData.replace(/"<");??
  103. ????strData=strData.replace(/"/g,?""");? ????strData=strData.replace(/'/g,?"'");??
  104. ????return?strData;?? }??

相应的调用代码如下:

?

?

js 代码?
  1. <script?type=< span="">"text/javascript">?? ??????
  2. ????var?service?;?? ????function?ajaxRequest()??
  3. ????{?? ????????var?url?=?"http://localhost:88/webservicedemo.asmx";??
  4. ?????????? ????????//设置webService传入参数??
  5. ????????//?? ????????//注意:??
  6. ????????//?? ????????//????调用webservice(如例子中的webservicedemo.asmx)??
  7. ????????//???????????HelloTo(String?name)???针对name参数必须写成name=wqj?,还有更多参数一样写,使用&符号分隔(name=11&age=20&.....),使用名称匹配?? ????????//???????????传入的参数数量可以不等于(多于或少于)方法要求的参数??
  8. ?????????? ??????????
  9. ????????var?para?=?"name=wqj";?? ??????????
  10. ????????var?op?=?{?? ????????????????????data:para,??
  11. ????????????????????onComplete:?showResponse,?? ????????????????????onFailure:showError,??
  12. ????????????????????update:'ajaxBack'?? ?????????????????};??
  13. ?? ????????service?=?new?WebService(url,"HelloTo",op);??
  14. ????????service.request();?? ????????return?false;??
  15. ????}?? ????function?showError(obj)??
  16. ????{?? ????????//obj?是一个xmlHttpRequest对象??
  17. ????????alert("error");?? ????}??
  18. ????function?showResponse(requestText,requestXML)?? ????{??
  19. ????????//requestText?返回的文本?? ????????//requestXML?返回的XML??
  20. ????????//?service.getBackData?就是取出返回的XML中,实际需要的数据?? ????????//经过测试兼容?IE,FF??
  21. ????????alert(service.getBackData(requestXML));?? ??????????
  22. ????}?? ????</script>??
JavaScriptSOAP.rar (47.1 KB) 描述: js调用webservice示例 JavaScriptSOAP(V2).rar (49.5 KB) 描述: js,sope第二版

热点排行