我想直接從Javascript調用SOAP WebService。我一直在尋找四周,但仍然無法有所作爲。我認爲我必須建立SOAP信封(見下文)。我也使用jQuery。如何從Javascript/jQuery調用SOAP WS
首先,我確定我可以調用位於別處的SOAP Webservice?那就是沒有像跨域限制那樣的限制。
另外我不確定什麼是我需要使用的正確的URL,SOAP服務是暴露使用Ladon,用於調試目的我已檢查WS可以很好用soapUI,這裏是我可以找到的URL:
- WSDL URL:
http://192.168.1.5/ws/MyWS/soap/description
//從我的理解,它不可能是這一個 - 服務端點:
http://192.168.1.5/ws/MyWS/soap
- 的SOAPAction:
http://192.168.1.5/ws/MyWS/soap/myOperation
我認爲我應該使用端點或SOAPAction,但它沒有工作。我可能會錯過這裏的東西,或者後來的Javascript是如此有缺陷以至於我無法確定。
現在,這裏是我的實際JS做呼叫(裏面有意見的一些問題):
<html>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<head>
<script type="text/javascript" src="ressources/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
// inspired by http://openlandscape.net/2009/09/25/call-soap-xm-web-services-with-jquery-ajax/
var soapServiceURL = 'http://192.168.1.5/ws/MyWS/soap/myOperation; // not sure what to put here from a LADON point of view
function callSOAPWS(myParameter)
{
var soapMessage =
'<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:LDetector"> \
<soapenv:Header/> \
<soapenv:Body> \
<urn:myOperation soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> \
<myParameter xsi:type="xsd:string">' + myParameter + '</myParameter > \
</urn:myOperation > \
</soapenv:Body> \
</soapenv:Envelope>';
alert("Check SOAP: [" + soapMessage + "]");
jQuery.ajax({
url: soapServiceURL,
type: "POST",
dataType: "xml",
data: soapMessage,
contentType: "text/xml; charset=\"utf-8\"",
//processData: false, // what is it for? may be should be true when using 'complete:' ?
//timeout: 5000,
// below I first try to have only 'complete:' then I tried to have 'success:' + 'error:', then the 3. Nothing seems to be ok. I do not find which one i should use.
complete: myCallback,
success: function(response){
document.getElementById('debug').innerHTML = document.getElementById('debug').innerHTML + '\n' + 'success!' + '\n';
alert("success!!!");
},
error: function(XMLHttpRequest,textStatus, errorThrown){
document.getElementById('debug').innerHTML = document.getElementById('debug').innerHTML + '\n' + 'error : ' + textStatus + '\n';
alert("error : " + textStatus);
}
});
alert('if we reach this line, is it a fail?!');
return false;
}
function myCallback(xmlHttpRequest, status)
{
jQuery(xmlHttpRequest.responseXML)
.find('detected')
.each(function()
{
var result = jQuery(this).find('result').text();
document.getElementById('debug').innerHTML = document.getElementById('debug').innerHTML + '\n' + result + '\n';
alert('ok : [' + result + ']');
});
}
// https://stackoverflow.com/questions/11916780/changing-getjson-to-jsonp?rq=1
jQuery(document).ready(function() {
callSOAPWS('this is a test');
});
</script>
<body>
<div id="debug" style="background-color:#EEEEEE; height:250px; width:600px; overflow:auto;"> </div>
</body>
</html>
問候
編輯:同時繼續嘗試和尋求答案,我有在那裏=>Simplest SOAP example Prestaul說:「除非Web服務與頁面位於同一個域,否則不能用直接的JavaScript來完成。」所以,也許我試圖做一些不可能的事情?這是它無法工作的原因嗎?
謝謝你,讓我錯誤地以爲使用SOAP採用了全JS客戶是一個變通。是的,我已經測試了我放在上面的代碼,它可以在同一臺服務器上使用。順便說一句,我還需要其他服務器能夠這樣做...我會看看你的建議。 – user1340802