如何从Javascript/jQuery调用SOAP WS

use*_*802 16 javascript jquery soap

我想直接从Javascript调用SOAP WebService.我一直在寻找,但仍然无法有所作为.我假设我必须构建SOAP enveloppe(见下文).我也使用jQuery.

首先,我确信我可以调用位于其他地方的SOAP Web服务吗?这就是没有像跨域限制这样的限制.

另外我不确定我需要使用什么是正确的URL,使用Ladon公开SOAP服务,出于调试目的我已经检查过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;">&nbsp;</div>

</body>
</html>
Run Code Online (Sandbox Code Playgroud)

最好的祝福

编辑:在继续尝试搜索答案时,我已经知道=> 最简单的SOAP示例,其中Prestaul说"除非Web服务与您的页面位于同一个域,否则无法使用直接JavaScript." 所以,也许我正在尝试做一些不可能的事情?这是它无法工作的原因吗?

Dar*_*rov 25

由于源于浏览器的原始策略限制,您无法发送跨域AJAX请求.为了使这项工作,包含jQuery代码的HTML页面必须与Web Service(http://192.168.1.5/ws/MyWS/)托管在同一个域中.

有一些解决方法涉及在服务器上使用JSONP,但由于您的Web服务是SOAP,因此无法工作.

使这项工作,如果你不能移动你的JavaScript在同一个域作为Web服务的唯一可靠方法是建立,将在同一个站点上托管的JavaScript代码的服务器端脚本,这将充当之间的桥梁2个域名.因此,您将向服务器端脚本发送一个AJAX请求,然后该脚本将调用远程Web服务并返回结果.


小智 11

这个怎么样?https://github.com/doedje/jquery.soap

看起来很简单.也许它会帮助你.

例:

$.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
}
});
Run Code Online (Sandbox Code Playgroud)

会导致

<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>
Run Code Online (Sandbox Code Playgroud)