基于SOAP的Web服务AJAX客户端
2015年12月14日
2015年12月15日添加WSDL
2015-12-16 添加SOAP与JSON转换
1 目标:浏览器客户端使用AJAX请求Web服务,传递参数,接受返回结果。
2 原理:使用POST传递SOAP格式的XML数据。
3 流程:发布WebService,查询WSDL,创建HTML和AJAX请求,测试。
参考:http://www.myexception.cn/h/1158587.html
3.1 发布WebService-基于SOAP的Web服务
参见:Java-webservice-CXF-SOAP服务.docx流程。
3.2 查询WSDL:查看服务的namespace和对应的xml地址。
3.3 创建HTML:指定WebService的URL和SOAP请求体。
注意:html文件放在WebContent根目录(保证处于同一域)。
参考:http://www.tuicool.com/articles/iueYNj
//index.html
<!DOCTYPE html>
<html>
<head>
<metacharset="UTF-8">
<title>Insert titlehere</title>
</head>
<scripttype="text/javascript"src="jquery-1.11.3.js"></script>
<body>
<buttontype="button" id="name">Web服务请求-SOAP</button >
<script type="text/javascript">
$(function() {
$("#name").click(function(){
var url = "/CXFSoapDemo/services/HelloWorld";
var soap = '<soapenv:Envelopexmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" '+
'xmlns:xsd="http://www.w3.org/2001/XMLSchema"'+
'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'+
'<soapenv:Bodyxmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tns="http://lee/"><tns:sayHello/></soapenv:Body></soapenv:Envelope>';
$.ajax({
url : url,//访问的url
dataType : 'xml',//返回的数据类型
type : 'post',//请求方式
contentType :'application/soap+xml;charset=UTF-8',
data : soap,//数据
success : function(data,status,
xhr) {
//对返回后的数据进行解析
$(data).find("return").each(
function(index,item) {
console.debug(item.innerHTML)
console.debug($(this));
});
},
error : function(xhr, status){
alert("出错了:" +status);
}
});
});
});
</script>
</body>
</html>
3.4 测试:发布服务并测试
点击页面,获取服务取返回结果。
4 方法:SOAP
参考:http://www.myexception.cn/web/677069.html
4.1 目标:将消息以xml形式的封装发送和接收解析。
4.2 原理:将消息以信的形式发送(包括信封、头、内容、异常等)。
4.3 流程:定义soap命名空间。配置信封(Envelope)、头(Header)、内容(Body)、异常(Fault)等。
//example.xml
<soap:Envelopexmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body xmlns:tns="http://lee/" >
<tns:sayHello/>
</soap:Body>
</soap:Envelope>
4.3.1定义soap命名空间:使用http://schemas.xmlsoap.org/soap/envelope/定义soap命名空间
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4.3.2配置消息内容:信封(必须)和内容(Body)是必须的。
<soap:Body xmlns:tns="http://lee/" >
<tns:sayHello/>
</soap:Body>
4.4 方法:SOAP
4.4.1组织结构:信封封装的消息。
Envelope:必须,使用http://schemas.xmlsoap.org/soap/envelope/定义
-|Header:可选
-|Body:必须,定义ws的命名空间
--|消息内容:必须,定义ws定义的方法。
-|Fault:可选。
4.4.2根据WSDL生成soap的ajax请求
4.4.2.1 查询WSDL:http://192.168.41.134:8080/CXFSoapDemo/services/HelloWorld?WSDL
This XML file does not appearto have any style information associated with it. The document tree is shownbelow.
<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://lee/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="HelloWorldImplService" targetNamespace="http://lee/">
<wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://lee/" elementFormDefault="unqualified" targetNamespace="http://lee/" version="1.0">
<xs:element name="say" type="tns:say"/>
<xs:element name="sayHello" type="tns:sayHello"/>
<xs:element name="sayHelloResponse" type="tns:sayHelloResponse"/>
<xs:element name="sayResponse" type="tns:sayResponse"/>
<xs:complexType name="sayHello">
<xs:sequence/>
</xs:complexType>
<xs:complexType name="sayHelloResponse">
<xs:sequence>
<xs:element minOccurs="0" name="return" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="say">
<xs:sequence>
<xs:element minOccurs="0" name="arg0" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="sayResponse">
<xs:sequence>
<xs:element minOccurs="0" name="return" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
</wsdl:types>
<wsdl:message name="sayResponse">
<wsdl:part element="tns:sayResponse" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:message name="say">
<wsdl:part element="tns:say" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:message name="sayHelloResponse">
<wsdl:part element="tns:sayHelloResponse" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:message name="sayHello">
<wsdl:part element="tns:sayHello" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:portType name="IHelloWorld">
<wsdl:operation name="sayHello">
<wsdl:input message="tns:sayHello" name="sayHello"></wsdl:input>
<wsdl:output message="tns:sayHelloResponse" name="sayHelloResponse"></wsdl:output>
</wsdl:operation>
<wsdl:operation name="say">
<wsdl:input message="tns:say" name="say"></wsdl:input>
<wsdl:output message="tns:sayResponse" name="sayResponse"></wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="HelloWorldImplServiceSoapBinding" type="tns:IHelloWorld">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"