Home >Web Front-end >JS Tutorial >Code for accessing WebService based on JQuery (accessible to Java [Xfire])

Code for accessing WebService based on JQuery (accessible to Java [Xfire])

高洛峰
高洛峰Original
2017-01-24 09:28:091185browse

I took a closer look at several people's examples and found the problem. As we all know, WebService complies with the SOAP protocol. Why are the examples all passing parameters in JSON format? net WebService is compatible with JSON format, while Java's is a standard WebService and is not compatible with JSON. It seems that the net has harmed everyone. So I carefully understood the WSDL file and made an example. Only the key code is placed below.

$(function () { 
$("#btnWs").click(btnAjaxPost); 
}); 

function btnAjaxPost(event) { 
$.ajax({ 
type: "POST", 
contentType:"text/xml", 
url:"http://*****/WebServiceTest/services/HelloWorldService", 
data:getPostData(),//这里不该用JSON格式 
dataType:'xml',//这里设成XML或者不设。设成JSON格式会让返回值变成NULL 
success: function(xml) { 
//对结果做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(&#39;error:&#39;+x.responseText); 
}, 
complete: function(x) { 
//alert(&#39;complete:&#39;+x.responseText); 
} 
}); 
} 
//定义满足SOAP协议的参数。 
function getPostData() 
{ 
//根据WSDL分析sayHelloWorld是方法名,parameters是传入参数名 
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; 
}

For more JQuery-based code for accessing WebService (accessible to Java[Xfire]), please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn