Ajax调用webservice

发布一下方法为webservice

public class SayHello {
	public String hello(String msg){
		return "say:"+msg;
	}
}


调用发布的webservice,弹出结果:say:hello world
<script>
var xhr;
var xmlDoc;
function get_mytatus()
{
	     if (window.XMLHttpRequest) {   
	          xhr = new XMLHttpRequest();   
	       } else {   
	          //  xhr = new ActiveXObject("Msxml2.XMLHTTP");   
	          xhr = new ActiveXObject("Microsoft.XMLHTTP");   
	       }   
	xhr.onreadystatechange=get_result; 
	xhr.open("POST", "http://localhost:8080/test/services/SayHello/hello",true);
	xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	xhr.send('msg=hello world');
}
function  get_result() 
{ 
if   (xhr.readyState == 4) { 
	  if(xhr.status == 200) 
	  { 
		  xmlDoc=new ActiveXObject("Microsoft.XMLDOM"); 
		  xmlDoc.async="false";
                    //加载返回的xml报文
		  xmlDoc.loadXML(xhr.responseText);
                    //解析返回的报文内容
		  var msg=xmlDoc.getElementsByTagName("ns:return")[0].childNodes[0].nodeValue; 
		  alert(msg);
	  }
	} 
} 
setInterval("get_mytatus()","3000");
</script>

你可能感兴趣的:(java,xml,Ajax,webservice,Microsoft)