您的位置:首页 > Web前端 > JavaScript

JS调用WebService

2009-09-10 15:06 736 查看
1, wsdl文件

<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions targetNamespace="http://service.fan.com" xmlns:soapenc12="http://www.w3.org/2003/05/soap-encoding" xmlns:tns="http://service.fan.com" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap11="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc11="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<wsdl:types>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://service.fan.com">
<xsd:element name="login">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="1" name="in0" nillable="true" type="xsd:string"/>//参数
<xsd:element maxOccurs="1" minOccurs="1" name="in1" nillable="true" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="loginResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="1" name="out" type="xsd:boolean"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
</wsdl:types>
<wsdl:message name="loginRequest">
<wsdl:part name="parameters" element="tns:login">
</wsdl:part>
</wsdl:message>
<wsdl:message name="loginResponse">
<wsdl:part name="parameters" element="tns:loginResponse">
</wsdl:part>
</wsdl:message>
<wsdl:portType name="LoginServicePortType">
<wsdl:operation name="login">
<wsdl:input name="loginRequest" message="tns:loginRequest">
</wsdl:input>
<wsdl:output name="loginResponse" message="tns:loginResponse">
</wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="LoginServiceHttpBinding" type="tns:LoginServicePortType">
<wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="login">//方法
<wsdlsoap:operation soapAction=""/>
<wsdl:input name="loginRequest">
<wsdlsoap:body use="literal"/>
</wsdl:input>
<wsdl:output name="loginResponse">
<wsdlsoap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="LoginService">
<wsdl:port name="LoginServiceHttpPort" binding="tns:LoginServiceHttpBinding">
<wsdlsoap:address location="http://192.168.4.124:8080/WebServiceServer/services/LoginService"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

2, JS调用

<html>
<head>
</head>
<body>
<span id="result">
</span>
<script language="javascript" type="text/javascript">

function RequestByPost(method,variable,value,url,targetNamespace)
{
this.xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
var data;
data = '<?xml version="1.0" encoding="utf-8"?>';
data += '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'
data += ' xmlns:xsd="http://www.w3.org/2001/XMLSchema" '
data += ' xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">';
data += '<soap:Body>';
data += '<'+method+' xmlns="'+targetNamespace+'">';
for(var i=0;i<variable.length;i++)
{
data += '<'+variable[i]+'>'+value[i]+'</'+variable[i]+'>';
}
data += '</'+method+'>';
data += '</soap:Body>';
data += '</soap:Envelope>';
xmlhttp.Open("POST",url, true); //若通过get方式传输只须将post改为get
xmlhttp.onreadystatechange = function (){
if (xmlhttp.readyState == 4)
{
document.getElementById("result").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.SetRequestHeader ("Content-Type","text/xml; charset=utf-8");
xmlhttp.SetRequestHeader ("Content-Length",getlen(data));
xmlhttp.SetRequestHeader ("SOAPAction",targetNamespace+method);
xmlhttp.Send(data);
}

function getlen(str)
{
var bytesCount=0;
for (var i = 0; i < str.length; i++)
{
var c = str.charAt(i);
if (/^[/u0000-/u00ff]$/.test(c))//匹配双字节
{
bytesCount += 1;
}
else
{
bytesCount += 2;
}
}
return bytesCount;
}

//test
RequestByPost("login",new Array('in0','in1'),new Array('admin','111'),
"http://192.168.4.124:8080/WebServiceServer/services/LoginService","http://service.fan.com/");
</script>

<body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: