如何从 Javascript/jQuery 调用 SOAP WS
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12796941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to call SOAP WS from Javascript/jQuery
提问by user1340802
I would like to call a SOAP WebService directly from Javascript. I have been looking all around but still is unable to have something working. I assumed that i must build the SOAP enveloppe (see below). I also use jQuery.
我想直接从 Javascript 调用 SOAP WebService。我一直在环顾四周,但仍然无法正常工作。我假设我必须构建 SOAP 信封(见下文)。我也使用 jQuery。
Firstly, I would be sure that I can call a SOAP Webservice located somewhere else ? that is there is no limitation like cross domain limitation.
首先,我确定我可以调用位于其他地方的 SOAP Web 服务?也就是说没有像跨域限制这样的限制。
Also I am not sure what is the right URL i need to use, SOAP Service is exposed using Ladon, for debugging purpose I have checked that the WS works well with soapUI, and here are the URLs that i can find :
此外,我不确定我需要使用的正确 URL 是什么,SOAP 服务是使用Ladon公开的,出于调试目的,我检查了 WS 是否与soapUI 配合良好,以下是我可以找到的 URL:
- WSDL URL :
http://192.168.1.5/ws/MyWS/soap/description
// from my understanding it can not be this one - service endpoints :
http://192.168.1.5/ws/MyWS/soap
- SOAPAction:
http://192.168.1.5/ws/MyWS/soap/myOperation
- WSDL URL :
http://192.168.1.5/ws/MyWS/soap/description
// 根据我的理解,它不可能是这个 - 服务端点:
http://192.168.1.5/ws/MyWS/soap
- 肥皂动作:
http://192.168.1.5/ws/MyWS/soap/myOperation
i think that i should use endpoint or SOAPAction but it did not work. I may miss something here or the later Javascript is so faulty that i can not be sure.
我认为我应该使用端点或 SOAPAction 但它没有用。我可能会在这里遗漏一些东西,或者后来的 Javascript 有问题,我无法确定。
Now here is my actual JS doing the call (there are some questions inside comments):
现在这是我实际调用的 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>
</head>
<body>
<div id="debug" style="background-color:#EEEEEE; height:250px; width:600px; overflow:auto;"> </div>
</body>
</html>
best regards
此致
EDIT:while continuing to try and search an answer, i have readed that => Simplest SOAP examplewhere Prestaul say "This cannot be done with straight JavaScript unless the web service is on the same domain as your page." so, maybe i'm trying to do something impossible? is it the reason why it can not work?
编辑:在继续尝试搜索答案的同时,我已经阅读了 => 最简单的 SOAP 示例,其中 Prestaul 说“除非 Web 服务与您的页面位于同一域,否则无法使用直接的 JavaScript 完成。” 所以,也许我正在尝试做一些不可能的事情?这是它不能工作的原因吗?
回答by Darin Dimitrov
You cannot send cross domain AJAX requests because of the same origin policyrestriction that's built into the browsers. In order to make this work your HTML page containing the jQuery code must be hosted on the same domain as the Web Service (http://192.168.1.5/ws/MyWS/
).
由于浏览器内置的同源策略限制,您无法发送跨域 AJAX 请求。为了使此工作正常运行,您的包含 jQuery 代码的 HTML 页面必须与 Web 服务 ( http://192.168.1.5/ws/MyWS/
)位于同一域中。
There are workarounds that involve using JSONPon the server, but since your web service is SOAP this cannot work.
有一些解决方法涉及在服务器上使用JSONP,但由于您的 Web 服务是 SOAP,因此无法正常工作。
The only reliable way to make this work if you cannot move your javascript on the same domain as the web service is to build a server side script that will be hosted on the same domain as the javascript code and that will act as a bridge between the 2 domains. So you would send an AJAX request to your server side script which in turn will invoke the remote web service and return the result.
如果您不能将您的 javascript 移动到与 Web 服务相同的域上,那么唯一可靠的方法是构建一个服务器端脚本,该脚本将托管在与 javascript 代码相同的域中,并充当2个域。因此,您将向服务器端脚本发送 AJAX 请求,该脚本又将调用远程 Web 服务并返回结果。
回答by geekasso
How about this? https://github.com/doedje/jquery.soap
这个怎么样?https://github.com/doedje/jquery.soap
Seems easy enough. Maybe it will help you.
似乎很容易。也许它会帮助你。
Example:
例子:
$.soap({
url: 'http://my.server.com/soapservices/',
method: 'helloWorld',
data: {
name: 'Remy Blom',
msg: 'Hi!'
},
success: function (soapResponse) {
// do stuff with soapResponse
// if you want to have the response as JSON use soapResponse.toJSON();
// or soapResponse.toString() to get XML string
// or soapResponse.toXML() to get XML DOM
},
error: function (SOAPResponse) {
// show error
}
});
will result in
会导致
<soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<helloWorld>
<name>Remy Blom</name>
<msg>Hi!</msg>
</helloWorld>
</soap:Body>
</soap:Envelope>
回答by AbdulAzizFarooqi
below code is working fine. may be it may help you.
下面的代码工作正常。也许它可以帮助你。
var SoaMessage = '<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/" >'
+ '<soapenv:Header/>'
+ '<soapenv:Body>'
+ '<myoperation soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns="http://MyService/"> '
+ ' <AgencyId xsi:type="xsd:string">ADBC</AgencyId >'
+ '</myoperation >'
+ '</soapenv:Body>'
+ '</soapenv:Envelope>';
var url = "http://XXXXXXX/XXX/XXXXX?wsdl";
$.support.cors = true;
$.ajax({
type: "POST",
url: url,
jsonpCallback: "MyCallbackDED",
dataType: "xml",
processData: false,
contentType: "text/xml; charset=\"utf-8\"",
success: function (msg) {
alert("suc: " + msg.tradeLicenseData.master[0].arabicAddress + ": " + msg.tradeLicenseData.master[0].arabicAddress);
},
error: function (msg) {
alert("Failed: " + msg.status + ": " + msg.statusText);
}
});