基于JQuery的訪問WebService的代碼(可訪問Java[Xfire])
更新時間:2010年11月19日 18:50:03 作者:
最近有些時間學(xué)習(xí)了下JQuery,發(fā)現(xiàn)有很多JQuery訪問.net WebService的例子。作為WebService這種接口應(yīng)該是通用的,為什么沒人關(guān)于Java的例子呢?這點引起我的興趣。
我仔細(xì)看看了看看幾個人的例子,發(fā)現(xiàn)了問題。眾所周知WebService是遵守SOAP協(xié)議的,為什么例子都是JSON格式的參數(shù)傳遞?net WebService兼容JSON格式,而Java的是標(biāo)準(zhǔn)WebService,不兼容JSON??磥韓et害了大家啊。于是我仔細(xì)了解了WSDL文件,做了個例子。下面只放關(guān)鍵代碼。
$(function () {
$("#btnWs").click(btnAjaxPost);
});
function btnAjaxPost(event) {
$.ajax({
type: "POST",
contentType:"text/xml",
url:"http://*****/WebServiceTest/services/HelloWorldService",
data:getPostData(),//這里不該用JSON格式
dataType:'xml',//這里設(shè)成XML或者不設(shè)。設(shè)成JSON格式會讓返回值變成NULL
success: function(xml) {
//對結(jié)果做XML解析。
//瀏覽器判斷 (IE和非IE完全不同)
if($.browser.msie){
$("#result").append(xml.getElementsByTagName("ns1:out")[0].childNodes[0].nodeValue+"<br/>");
}
else{
$(xml).find("out").each(function(){
$("#result").append($(this).text()+"<br/>");
})
}
},
error: function(x, e) {
alert('error:'+x.responseText);
},
complete: function(x) {
//alert('complete:'+x.responseText);
}
});
}
//定義滿足SOAP協(xié)議的參數(shù)。
function getPostData()
{
//根據(jù)WSDL分析sayHelloWorld是方法名,parameters是傳入?yún)?shù)名
var postdata="<?xml version=\"1.0\" encoding=\"utf-8\"?>";
postdata+="<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">";
postdata+="<soap:Body><sayHelloWorld xmlns=\"http://tempuri.org/\">";
postdata+="<parameters>"+$("#txtName").val()+"</parameters>";
postdata+="</sayHelloWorld></soap:Body>";
postdata+="</soap:Envelope>";
return postdata;
}
完整例子SVN地址:http://theyounglearningmaterials.googlecode.com/svn/trunk/JavaWebServices/WebServiceTest/
我以后所有學(xué)習(xí)的例子都會放在 http://theyounglearningmaterials.googlecode.com/svn/trunk/里面,方便管理防止丟失。
復(fù)制代碼 代碼如下:
$(function () {
$("#btnWs").click(btnAjaxPost);
});
function btnAjaxPost(event) {
$.ajax({
type: "POST",
contentType:"text/xml",
url:"http://*****/WebServiceTest/services/HelloWorldService",
data:getPostData(),//這里不該用JSON格式
dataType:'xml',//這里設(shè)成XML或者不設(shè)。設(shè)成JSON格式會讓返回值變成NULL
success: function(xml) {
//對結(jié)果做XML解析。
//瀏覽器判斷 (IE和非IE完全不同)
if($.browser.msie){
$("#result").append(xml.getElementsByTagName("ns1:out")[0].childNodes[0].nodeValue+"<br/>");
}
else{
$(xml).find("out").each(function(){
$("#result").append($(this).text()+"<br/>");
})
}
},
error: function(x, e) {
alert('error:'+x.responseText);
},
complete: function(x) {
//alert('complete:'+x.responseText);
}
});
}
//定義滿足SOAP協(xié)議的參數(shù)。
function getPostData()
{
//根據(jù)WSDL分析sayHelloWorld是方法名,parameters是傳入?yún)?shù)名
var postdata="<?xml version=\"1.0\" encoding=\"utf-8\"?>";
postdata+="<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">";
postdata+="<soap:Body><sayHelloWorld xmlns=\"http://tempuri.org/\">";
postdata+="<parameters>"+$("#txtName").val()+"</parameters>";
postdata+="</sayHelloWorld></soap:Body>";
postdata+="</soap:Envelope>";
return postdata;
}
完整例子SVN地址:http://theyounglearningmaterials.googlecode.com/svn/trunk/JavaWebServices/WebServiceTest/
我以后所有學(xué)習(xí)的例子都會放在 http://theyounglearningmaterials.googlecode.com/svn/trunk/里面,方便管理防止丟失。
相關(guān)文章
jQuery使用attr()方法同時設(shè)置多個屬性值用法實例
這篇文章主要介紹了jQuery使用attr()方法同時設(shè)置多個屬性值的用法,實例分析了jQuery中attr方法實現(xiàn)多個屬性設(shè)置的技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-03-03jQuery 獲取跨域XML(RSS)數(shù)據(jù)的相關(guān)總結(jié)分析
下面小編就為大家?guī)硪黄猨Query 獲取跨域XML(RSS)數(shù)據(jù)的相關(guān)總結(jié)分析。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-05-05Spring MVC中Ajax實現(xiàn)二級聯(lián)動的簡單實例
下面小編就為大家?guī)硪黄猄pring MVC中Ajax實現(xiàn)二級聯(lián)動的簡單實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-07-07jQuery根據(jù)ID獲取input、checkbox、radio、select的示例
獲取input、checkbox、radio、select的方法有很多,下面本例為大家介紹下根據(jù)ID來獲取2014-08-08基于jquery的文本框與autocomplete結(jié)合使用(asp.net+json)
基于jquery的文本框與autocomplete結(jié)合使用示例代碼,需要的朋友可以參考下2012-05-05