欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于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)鍵代碼。
復(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)文章

最新評論