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

使用ajax调用webservice

发布时间:2020-12-15 22:05:46 所属栏目:百科 来源:网络整理
导读:注意,使用ajax调用webservice时,尽量使用ie浏览器,如果使用chrome或者是firefox浏览器,很可能会出现以下异常 2013-6-1 11:10:02 com.sun.xml.internal.ws.transport.http.server.WSHttpHandler handleExchange警告: Cannot handle HTTP method: OPTIONS

注意,使用ajax调用webservice时,尽量使用ie浏览器,如果使用chrome或者是firefox浏览器,很可能会出现以下异常

2013-6-1 11:10:02 com.sun.xml.internal.ws.transport.http.server.WSHttpHandler handleExchange
警告: Cannot handle HTTP method: OPTIONS

1、服务器端代码的书写(可以参考使用jdk调用webservice中的代码,两者是基本相同的)

2、ajax_webservice.html

<html>
	<head>
		<title>通过ajax调用WebService服务</title>
		<script>
			 function getXhr(){
				var xhr = null;
				if(window.XMLHttpRequest){
					//非ie浏览器
					xhr = new XMLHttpRequest();
				}else{
					//ie浏览器
					xhr = new ActiveXObject('Microsoft.XMLHttp');
				}
				return xhr;
			}
          var xhr =getXhr();
			function sendMsg(){
				var name = document.getElementById('name').value;
				//服务的地址
				var wsUrl = 'http://127.0.0.1:6790/hello';
				
				//请求体
				 var soap= '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="http://webservice.njupt.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'
             +'<soapenv:Body><q0:sayHello><arg0>'+name+'</arg0> </q0:sayHello></soapenv:Body></soapenv:Envelope>';
             			 
				//打开连接
				xhr.open('POST',wsUrl,true);
				
				//重新设置请求头
				xhr.setRequestHeader("Content-Type","text/xml;charset=UTF-8");
				
				//设置回调函数
				xhr.onreadystatechange = _back;
				
				//发送请求
				xhr.send(soap);
			}
			
			function _back(){
				if(xhr.readyState == 4){
					if(xhr.status == 200){
							//alert('调用Webservice成功了');
							var ret = xhr.responseXML;
							var msg = ret.getElementsByTagName('return')[0];
							document.getElementById('showInfo').innerHTML = msg.text;
							//alert(msg.text);
						}
				}
			}
		</script>
	</head>
	<body>
			<input type="button" value="发送SOAP请求" onclick="sendMsg();">
			<input type="text" id="name">
			<div id="showInfo">
			</div>
	</body>
</html>

(编辑:李大同)

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

    推荐文章
      热点阅读