簡體   English   中英

如何從 Javascript/jQuery 調用 SOAP WS

[英]How to call SOAP WS from Javascript/jQuery

我想直接從 Javascript 調用 SOAP WebService。 我一直在環顧四周,但仍然無法正常工作。 我假設我必須構建 SOAP 信封(見下文)。 我也使用 jQuery。

首先,我確定我可以調用位於其他地方的 SOAP Web 服務? 也就是說沒有像跨域限制這樣的限制。

此外,我不確定我需要使用的正確 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 : http://192.168.1.5/ws/MyWS/soap
  • SOAPAction: http://192.168.1.5/ws/MyWS/soap/myOperation : 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>
</head>
<body>

<div id="debug" style="background-color:#EEEEEE; height:250px; width:600px; overflow:auto;">&nbsp;</div>

</body>
</html>

最好的祝福

編輯:在繼續嘗試搜索答案的同時,我已經閱讀了 => 最簡單的 SOAP 示例,其中 Prestaul 說“除非 Web 服務與您的頁面位於同一域,否則無法使用直接的 JavaScript 完成。” 所以,也許我正在嘗試做一些不可能的事情? 這是它不能工作的原因嗎?

由於瀏覽器內置的同源策略限制,您無法發送跨域 AJAX 請求。 為了使此工作正常運行,您的包含 jQuery 代碼的 HTML 頁面必須與 Web 服務 ( http://192.168.1.5/ws/MyWS/ ) 托管在同一域中。

有一些解決方法涉及在服務器上使用JSONP ,但由於您的 Web 服務是 SOAP,因此無法正常工作。

如果您不能將您的 javascript 移動到與 Web 服務相同的域上,那么唯一可靠的方法是構建一個服務器端腳本,該腳本將托管在與 javascript 代碼相同的域中,並充當2個域。 因此,您將向服務器端腳本發送 AJAX 請求,該腳本又將調用遠程 Web 服務並返回結果。

這個怎么樣? 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
}
});

會導致

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

下面的代碼工作正常。 也許它可以幫助你。

    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);
        }

    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM