软件工程实践者

统计

留言簿

友情链结

阅读排行榜

评论排行榜

JavaScript调用Web service

要调用的Web服务是求两个整数和,并返回结果。

服务的WSDL文件内容如下:
<?xml version="1.0" encoding="utf-8" ?> 
<wsdl:definitions
    xmlns:http="http://schemas.xmlsoap.org/wsdl/http/"
    xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
    xmlns:s="http://www.w3.org/2001/XMLSchema" 
    xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" 
    xmlns:tns="http://tempuri.org/"  
    xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" 
    xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" 
    targetNamespace="http://tempuri.org/" 
    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
  <wsdl:types>
     <s:schema elementFormDefault="qualified"  
        targetNamespace="http://tempuri.org/">
        <s:element name="AddTwoIntegers">
            <s:complexType>
               <s:sequence>  
                  <s:elementminOccurs="1" maxOccurs="1" name="IntegerOne" type="s:int" />    
                  <s:elementminOccurs="1" maxOccurs="1" name="IntegerTwo" type="s:int" />   
               </s:sequence>
            </s:complexType>
        </s:element>
        <s:element name="AddTwoIntegersResponse">
            <s:complexType>
                <s:sequence> 
                   <s:elementminOccurs="1" maxOccurs="1" name="AddTwoIntegersResult" type="s:int" />    
                </s:sequence>
            </s:complexType>
        </s:element>
     </s:schema>
  </wsdl:types>
  <wsdl:message name="AddTwoIntegersSoapIn"> 
      <wsdl:part name="parameters" element="tns:AddTwoIntegers" />
  </wsdl:message>
  <wsdl:message name="AddTwoIntegersSoapOut">  
      <wsdl:part name="parameters" element="tns:AddTwoIntegersResponse" />   
   </wsdl:message>
   <wsdl:portType name="SimpleServiceSoap">
       <wsdl:operation name="AddTwoIntegers">
          <wsdl:input message="tns:AddTwoIntegersSoapIn" />
          <wsdl:output message="tns:AddTwoIntegersSoapOut" />    
       </wsdl:operation> 
   </wsdl:portType>

   <wsdl:binding name="SimpleServiceSoap" type="tns:SimpleServiceSoap"> 
       <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" /> 
       <wsdl:operation name="AddTwoIntegers">   
           <soap:operation soapAction="http://tempuri.org/AddTwoIntegers" style="document" /> 
           <wsdl:input>  
               <soap:body use="literal" />    
           </wsdl:input>
           <wsdl:output>  
               <soap:body use="literal" />    
           </wsdl:output>
       </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="SimpleService"> 
      <documentation xmlns="http://schemas.xmlsoap.org/wsdl/" /> 
      <wsdl:port name="SimpleServiceSoap" binding="tns:SimpleServiceSoap">  
          <soap:address location="http://localhost/Develop.NET/Home.Develop.WebServices/SimpleService.asmx"/>  
      </wsdl:port>  
  </wsdl:service> 
</wsdl:definitions>


javaScript代码中利用了MS的HTTP代理对象XMLHTTP,在Mozilla's Web brower中相应的组件是XMLHttpRequest,他们都提供了类似的方法来完成soap请求。下面的代码用的是IE中的XMLHTTP对象。代码假定调用过程中没有Fault。
function fncAddTwoIntegers(a, b)
{    
    var oXmlHttp = new ActiveXObject("MSXML2.XMLHTTP");      
    oXmlHttp.open("POST", "http://localhost/Develop.NET/Home.Develop.WebServices/SimpleService.asmx'", false);
    oXmlHttp.setRequestHeader("Content-Type", "text/xml");    
    oXmlHttp.setRequestHeader("SOAPAction", "http://tempuri.org/AddTwoIntegers");

    oXmlHttp.send("      
<soap:Envelopexmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>
<soap:Body> <AddTwoIntegersxmlns='http://tempuri.org/'> <IntegerOne>" + a + "</IntegerOne> <IntegerTwo>" + b + "</IntegerTwo> </AddTwoIntegers>
</soap:Body> </soap:Envelope>"); return oXmlHttp.responseXML.selectSingleNode("//AddTwoIntegersResult").text; }


原文英文出自:http://builder.com.com/5100-6371_14-5887775.html?tag=nl.e601

posted on 2006-01-05 20:01 软件工程实践者 阅读(1204) 评论(3)  编辑  收藏 所属分类: Web service

评论

# re: JavaScript调用Web service 2010-12-06 17:53 你爹

傻逼  回复  更多评论   

# re: JavaScript调用Web service 2010-12-06 17:54 你爹

傻逼,不好使  回复  更多评论   

# re: JavaScript调用Web service 2010-12-06 17:54 你爹

傻逼  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航: