ajax调用webservice

<html>
	<head>
		<title>通过ajax来调用webservice</title>
			<script type="text/javascript">
				var xmlHttp = false;
				function sendMsg()
				{
					try
					{
					  xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
					} catch (e)
					{
					  try
					  {
					    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
					  } catch (e2)
					  {
					    xmlHttp = false;
					  }
					}
					if (!xmlHttp && typeof XMLHttpRequest != 'undefined')
					{
	  				xmlHttp = new XMLHttpRequest();
					}
					alert("pass");
					//服务的地址
					var name = document.getElementById("name").value;
					alert(name);
					var wsUrl ="http://192.168.1.192:6789/hello";
					//请求体
					var soap ='<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="http://webservice.powerjunior.org/" 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>';
					//打开链接
					xmlHttp.open('POST',wsUrl,true);
					//重新设置请求头
					xmlHttp.setRequestHeader("Content-Type","text/xml;charset=UTF-8");
					//设置回调函数
					xmlHttp.onreadystatechange=_back;
					//发送请求
					xmlHttp.send(soap);
				}
				//回调函数
				function _back()
				{
					if(xmlHttp.readyState == 4)
					{
						if(xmlHttp.status == 200)
						{
							alert('调用webservice成功!');
							var ret = xmlHttp.responseXML;
							var msg = ret.getElementsByTagName("return")[0];
							document.getElementById("showInfo").innerHTML = msg.text;
						}
					}
				}
			</script>
	</head>
	<body>
		<input type ="button" onclick="sendMsg();" value="调用webservice">
		<input type="text" id="name" />
		<div id="showInfo"></div>
	</body>
</html>

相关推荐