基于SOAP的Web服务AJAX客户端

本文详细介绍了如何使用AJAX实现基于SOAP的Web服务客户端,包括发布WebService、查询WSDL、创建HTML和AJAX请求的步骤,以及SOAP消息的组织结构和原理。示例中展示了使用jQuery组织SOAP AJAX请求的过程,并提到了SOAP与JSON转换的方法。
摘要由CSDN通过智能技术生成

基于SOAP的Web服务AJAX客户端

sf2gis@163.com

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"

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

弗里曼的小伙伴

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值