2012-10-09 38 views
13

我想直接從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;">&nbsp;</div> 

</body> 
</html> 

問候

編輯:同時繼續嘗試和尋求答案,我有在那裏=>Simplest SOAP example Prestaul說:「除非Web服務與頁面位於同一個域,否則不能用直接的JavaScript來完成。」所以,也許我試圖做一些不可能的事情?這是它無法工作的原因嗎?

回答

20

由於內置在瀏覽器中的same origin policy限制,您無法發送跨域AJAX請求。爲了完成這項工作,包含jQuery代碼的HTML頁面必須與Web Service位於同一個域(http://192.168.1.5/ws/MyWS/)。

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

如果您無法將您的JavaScript移動到與Web服務相同的域上,則唯一可靠的方法就是使該工作成爲可能,即構建服務器端腳本,該腳本將託管在與JavaScript代碼相同的域中,這兩個領域之間的橋樑。所以你會發送一個AJAX請求到你的服務器端腳本,這個腳本又會調用遠程web服務並返回結果。

+0

謝謝你,讓我錯誤地以爲使用SOAP採用了全JS客戶是一個變通。是的,我已經測試了我放在上面的代碼,它可以在同一臺服務器上使用。順便說一句,我還需要其他服務器能夠這樣做...我會看看你的建議。 – user1340802

1

下面的代碼工作正常。可能會對你有所幫助。

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

    }); 
+2

這不起作用 – Mukus

+0

不能工作.. –

8

這個怎麼樣? 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> 
+0

與svc文件一樣https:// localhost:1303/BbsService.svc我們該如何使用它? – arslanaybars