使用ajax调用webservice
2013-06-01 11:15
337 查看
注意,使用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>
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>
相关文章推荐
- Webservice的cxf开发_使用spring发布服务+ajax调用服务
- ASP.NET2.0下使用AJAX调用Webservice的方法
- JS使用AJAX调用.net WebService
- 使用ajax调用webservice加载table
- ASP.Net Ajax应用初步:使用AJAX调用WebService
- ASP.NET2.0下使用AJAX调用Webservice的方法
- 使用 XMLHttpRequest 实现 Ajax 四:调用 Webservice
- JQuery的Ajax使用Get,Post方法调用C#WebService并返回数据
- 使用ASP.NET AJAX调用后台WebService优化网站性能实例
- 使用ajax调用webservice服务的方法
- 使用ajax调用webservice
- jQuery使用Ajax方法调用WebService
- 使用jquery的ajax方法调用webservice服务
- ASP.Net Ajax应用初步:使用AJAX调用WebService
- [WebMethod]的使用,ajax调用[WebMethod]的使用,webservice(web服务) asmx的使用,ajax调用[WebMethod]进行json传输
- 使用ajax调用webservice
- 第三部分 01 使用ASP.NET AJAX异步调用WebService(全)
- (webservice,ajax,jmail)Tip:动态调用webservice,ajaxpro的使用要点以及使用jmail接口使用注意事项
- 在Android中使用Android Ksoap2调用WebService
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(9):服务器端和客户端数据类型的自动转换:DataTable和DataSet