如何从Javascript / jQuery调用SOAP WS [英] How to call SOAP WS from Javascript/jQuery

查看:101
本文介绍了如何从Javascript / jQuery调用SOAP WS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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.

另外我不确定我需要使用什么是正确的URL,使用 Ladon ,出于调试目的,我已经检查过WS在soapUI上运行良好,这里是我可以使用的URL查找:

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 :


  • 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

  • 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

我认为我应该使用端点或SOAPAction,但它不起作用。我可能会错过这里或后来的Javascript是如此错误以至于我无法确定。

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.

现在这里是我的实际JS正在进行调用(注释中有一些问题) ):

Now here is my actual JS doing the call (there are some questions inside comments):

<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>

最好的问候

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

while continuing to try and search an answer, i have readed that => Simplest SOAP example where 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?

推荐答案

由于同源政策限制内置于浏览器中。为了使这项工作,包含jQuery代码的HTML页面必须与Web服务托管在同一个域中( http://192.168.1.5/ws/MyWS/ )。

You cannot send cross domain AJAX requests because of the same origin policy restriction 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/).

有一些解决方法涉及使用 JSONP 服务器,但由于你的Web服务是SOAP,这无法工作。

There are workarounds that involve using JSONP on the server, but since your web service is SOAP this cannot work.

如果你不能在与web服务相同的域上移动你的javascript,这是唯一可行的方法是构建一个服务器端脚本,它将与javascript代码托管在同一个域中,并充当2个域之间的桥梁。因此,您将向服务器端脚本发送一个AJAX请求,然后该脚本将调用远程Web服务并返回结果。

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 / jQuery调用SOAP WS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆