ajax请求代码:
<script>
var xhr = new XMLHttpRequest();
function wsConnection(){
//创建XMLHttp的对象
//服务的地址
var urlMsg = 'http://192.168.1.174:6789/hello';
//请求体
var soap = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="http://score.it.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema'+ 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><q0:sayHello> <arg0>aaa</arg0> <arg1>123</arg1></q0:sayHello></soapenv:Body> </soapenv:Envelope>';
//打开连接-true :表示为异步请求
xhr.open('POST',urlMsg,true);
xhr.crossDomain=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('成功响应');
var ret = xhr.responseXML;
var msg = ret.getElementsByTagName("return")[0];
alert(msg.text)
}
}
}
</script>
<body>
<input type="button" value="发送请求" onclick="wsConnection()"/>
<input type="text" id="response"/>
</body>
主程序与控制台输出代码:
@WebService
public class HelloService {
public String sayHello(String name,int num){
System.out.println("客户请求来了..................");
return "Hello "+name;
}
//发布时,此方法不显示
@WebMethod(exclude=true)
public String sayHi(String name){
return "Hi "+name;
}
public static void main(String[] args) {
/**
* 参数1:服务发布地址
* 参数2:服务实现者
*/
Endpoint.publish("http://192.168.1.174:6789/hello", new HelloService());
System.out.println("我是由主线程执行");
}
}
控制台:
五月 29, 2015 9:30:06 上午 com.sun.xml.ws.transport.http.server.WSHttpHandler handleExchange
警告: 无法处理 HTTP 方法: OPTIONS