客户端访问WebService的WSDL文件

问:

Hi  All

I have requirment to access web service wsdl (newCarConfig.cfc?wsdl) file through javascript with out using webservice.htc file. Is there any way to achive this functionality

Thanks in advance


答:

you can read the service wsdl just as a XML file, using AJAX can achieve easily.

The following example (from http://www.captain.at/howto-ajax-process-xml.php) demonstrates a simple AJAX JavaScript for requesting XML data from a server and displaying the response in a HTML-table. The server-side script is a simple PHP script, which actually just sends some XML data. I think it can lead you to achieve what your needs: 

复制代码
<script type="text/javascript" language="javascript">
   
var http_request = false;
   
function makeRequest(url, parameters) {
      http_request 
= false;
      
if (window.XMLHttpRequest) // Mozilla, Safari,
         http_request = new XMLHttpRequest();
         
if (http_request.overrideMimeType) {
            http_request.overrideMimeType(
'text/xml');
         }

      }
 else if (window.ActiveXObject) // IE
         try {
            http_request 
= new ActiveXObject("Msxml2.XMLHTTP");
         }
 catch (e) {
            
try {
               http_request 
= new ActiveXObject("Microsoft.XMLHTTP");
            }
 catch (e) {}
         }

      }

      
if (!http_request) {
         alert(
'Cannot create XMLHTTP instance');
         
return false;
      }

      http_request.onreadystatechange 
= alertContents;
      http_request.open(
'GET', url + parameters, true);
      http_request.send(
null);
   }


   
function alertContents() {
      
if (http_request.readyState == 4{
         
if (http_request.status == 200{

            
var xmldoc = http_request.responseXML;
            
var root = xmldoc.getElementsByTagName('root').item(0);
            
            
for (var iNode = 0; iNode < root.childNodes.length; iNode++{
               
var node = root.childNodes.item(iNode);
               
for (i = 0; i < node.childNodes.length; i++{
                  
var sibl = node.childNodes.item(i);
                  
var len = parseInt(sibl.childNodes.length / 2);
                  
var arr = new Array(len);
                  
var cnt = 0;
                  
for (x = 0; x < sibl.childNodes.length; x++{
                     
var sibl2 = sibl.childNodes.item(x);
                     
var sibl3;
                     
if (sibl2.childNodes.length > 0{
                        sibl3 
= sibl2.childNodes.item(0);
                        arr[cnt] 
= sibl3.data;   
                        cnt
++;
                     }

                  }

                  addrow(
"mytable", arr);
               }

            }

         }
 else {
            alert(
'There was a problem with the request.');
         }

      }

   }

   
function do_xml() {
      makeRequest(
'xml.php''?test=2');
   }

   
function addrow(tablename, arr) {
   
var tbl = document.getElementById(tablename);
   
var lastRow = tbl.rows.length;
   
var row = tbl.insertRow(lastRow);
      
for (r = 0; r < arr.length; r++{   
         
var cell = row.insertCell(r);
         cell.innerHTML 
= arr[r];
      }

   }


</script>

<input type="button" name="button" value="GET XML" 
   onclick
="javascript:do_xml();">

<br><br>
Table filled with data requested from the server:
<br>
<table border="1" id="mytable">
</table>
复制代码

posted @   LanceZhang  阅读(6114)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示