加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

WebService-CXF-jQuery跨域访问

发布时间:2020-12-16 23:36:21 所属栏目:安全 来源:网络整理
导读:通过JS来调用WebService: 通过jQuery的ajax方法向服务器发送xml数据。 必须要先了解xml的数据格式,通过拦截器获

通过JS来调用WebService:


通过jQuery的ajax方法向服务器发送xml数据。
必须要先了解xml的数据格式,通过拦截器获取。
?可以从LoggingInInterceptor中获取发出的数据格式。
?可以从LoggingOutInterceptor中获取返回的数据。
导入jQuery1.5.js文件,放到js/jquery1.5.js

通过Ajax访问WebService


l使用jQuery-本域
l$.ajax({
?url:..
?type:’post’,
?dataType:’xml’,
?contentType:’application/soap+xml;charset=“UTF-8”’
?data:someXml,
?Success:fun()…
l})


Jquery是跨域请求,必须使用jsonp,jsonp,其实就是在服务端又执行一段java代码。如此有点太过麻烦。不如使用原生的ajax.



在本域使用jquery访问: --查询所有用户:




<script type="text/javascript">
  	$(function(){
  		$("#btn1").click(function(){
  			var url = "http://localhost:7777/ws2/ws/user";
  			var soap = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" '+
  					   'xmlns:q0="http://service.ws2.itcast.cn/" '+
  					   'xmlns:xsd="http://www.w3.org/2001/XMLSchema" '+
  					   'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'+
  					   '<soapenv:Body><q0:getUsers/></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(){
  						var nm = $(this).find("name").text();
  						var age = $(this).find("age").text();
  						alert(nm+","+age);
  					});
  				},error:function(xhr,status){
  					alert("出错了:"+status);
  				}
  			});
  		});
  	});
  </script>



以下是jsclient.jsp的源代码:

<html>
  <head>
  	<script type="text/javascript" 
  			src="<c:url value='/js/jquery-1.5.js'/>"></script>
  </head>
  <body>
    	<label for="name">姓名:</label>
    	<input type="text" id="name" name="name"/>
    	<br/>
    	<a href="#" id="ok">确定</a>
  </body>
  <script type="text/javascript">
  	$(function(){
		$("#ok").click(function(){
			var val = $("#name").val();
			if($.trim(val)==""){
				alert("请输入名称");
				return;
			}
			var str = '<?xml version="1.0" encoding="UTF-8"?>'+
					  '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'+
					  '<soap:Body><ns2:sayHello xmlns:ns2="http://first.cxf.itcast.com/">'+
					  '<arg0>'+val+'</arg0>'+
					  '</ns2:sayHello></soap:Body></soap:Envelope>';
			$.ajax({
				contentType:'application/xml;charset="UTF-8"',dataType:'xml',type:'post',url:'http://localhost:9999/cxf2.4_spring_web/ws/helloworld',//直接发向这个地址
				data:str,success:function(data){
					//$(data).find("return").each(function(){
					//	alert($(this).text());
					//});					//使用上面的方法也是可以的
					var ss = $(data).find("return").first().text();
					$("<div>").html(ss)
					    .css("border","1px solid blue")
					    .css({width:'50%'}).
						appendTo($("body"));
					$("#name").val("");
				}
			},"xml");
		});
  	});
  </script>
</html>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读