基于JQuery的訪問WebService的代碼(可訪問Java[Xfire])
更新時(shí)間:2010年11月19日 18:50:03 作者:
最近有些時(shí)間學(xué)習(xí)了下JQuery,發(fā)現(xiàn)有很多JQuery訪問.net WebService的例子。作為WebService這種接口應(yīng)該是通用的,為什么沒人關(guān)于Java的例子呢?這點(diǎn)引起我的興趣。
我仔細(xì)看看了看看幾個(gè)人的例子,發(fā)現(xiàn)了問題。眾所周知WebService是遵守SOAP協(xié)議的,為什么例子都是JSON格式的參數(shù)傳遞?net WebService兼容JSON格式,而Java的是標(biāo)準(zhǔn)WebService,不兼容JSON??磥韓et害了大家啊。于是我仔細(xì)了解了WSDL文件,做了個(gè)例子。下面只放關(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實(shí)現(xiàn)圖片上傳預(yù)覽與刪除操作
這篇文章主要為大家詳細(xì)介紹了基于JQuery實(shí)現(xiàn)圖片上傳預(yù)覽與刪除操作的相關(guān)代碼,感興趣的小伙伴們可以參考一下2016-05-05
幻燈片帶網(wǎng)頁設(shè)計(jì)中的20個(gè)奇妙應(yīng)用示例小結(jié)
幻燈片效果在網(wǎng)站中的使用非常流行,使用幻燈片效果既能在有限的網(wǎng)頁空間內(nèi)展示更多的內(nèi)容,又能增強(qiáng)視覺趣味,網(wǎng)上眾多的幻燈片插件資源也使得幻燈片的實(shí)現(xiàn)變得十分簡單2012-05-05
jQuery--遍歷操作實(shí)例小結(jié)【后代、同胞及過濾】
這篇文章主要介紹了jQuery 遍歷操作,結(jié)合實(shí)例形式詳細(xì)分析了jQuery針對后代、同胞及過濾遍歷的相關(guān)函數(shù)、操作技巧與使用注意事項(xiàng),需要的朋友可以參考下2020-05-05
jquery創(chuàng)建一個(gè)ajax關(guān)鍵詞數(shù)據(jù)搜索實(shí)現(xiàn)思路
我們經(jīng)常需要在前臺頁面輸入關(guān)鍵詞進(jìn)行數(shù)據(jù)的搜索這已經(jīng)成為了一種習(xí)慣今天給大家分享一下如何使用 jQuery,MySQL和Ajax創(chuàng)建簡單和有吸引力的Ajax搜索,感興趣的你可不要錯(cuò)過了哈2013-02-02

