2012-10-09 47 views
13

Tôi muốn gọi một SOAP WebService trực tiếp từ Javascript. Tôi đã được tìm kiếm tất cả các xung quanh nhưng vẫn không thể có một cái gì đó làm việc. Tôi giả định rằng tôi phải xây dựng phong bì SOAP (xem bên dưới). Tôi cũng sử dụng jQuery.Cách gọi SOAP WS từ Javascript/jQuery

Thứ nhất, tôi chắc chắn rằng tôi có thể gọi một WebService SOAP ở một nơi khác? đó là không có giới hạn như giới hạn miền chéo.

Ngoài ra tôi không chắc URL nào tôi cần sử dụng, Dịch vụ SOAP được hiển thị bằng cách sử dụng Ladon, vì mục đích gỡ lỗi Tôi đã kiểm tra xem WS có hoạt động tốt với soapUI không và đây là các URL mà tôi có thể tìm thấy:

  • URL WSDL: http://192.168.1.5/ws/MyWS/soap/description // từ hiểu biết của tôi nó không thể là một
  • dịch vụ này điểm cuối: http://192.168.1.5/ws/MyWS/soap
  • SOAPAction: http://192.168.1.5/ws/MyWS/soap/myOperation

tôi nghĩ rằng tôi nên sử dụng điểm cuối hoặc SOAPAction nhưng nó không hoạt động. Tôi có thể bỏ lỡ một cái gì đó ở đây hoặc Javascript sau đó là như vậy bị lỗi mà tôi không thể chắc chắn.

Bây giờ đây là JS thực tế của tôi làm cuộc gọi (có một số câu hỏi trong ý kiến):

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

tốt nhất liên quan

EDIT: trong khi tiếp tục cố gắng và tìm kiếm một câu trả lời, tôi có đọc rằng =>Simplest SOAP example nơi Prestaul nói "Điều này không thể được thực hiện với JavaScript thẳng trừ khi dịch vụ web nằm trên cùng một tên miền với trang của bạn." vì vậy, có lẽ tôi đang cố gắng làm điều gì đó không thể? đó có phải là lý do tại sao nó không thể hoạt động?

Trả lời

20

Bạn không thể gửi yêu cầu AJAX miền chéo vì giới hạn same origin policy được tích hợp vào trình duyệt. Để làm cho công việc này, trang HTML của bạn chứa mã jQuery phải được lưu trữ trên cùng một miền với Dịch vụ Web (http://192.168.1.5/ws/MyWS/).

Có các cách giải quyết liên quan đến việc sử dụng JSONP trên máy chủ nhưng vì dịch vụ web của bạn là SOAP, điều này không thể hoạt động.

Cách đáng tin cậy duy nhất để thực hiện tác vụ này nếu bạn không thể di chuyển javascript của mình trên cùng một tên miền như dịch vụ web là xây dựng tập lệnh phía máy chủ sẽ được lưu trữ trên cùng một miền với mã javascript và sẽ hoạt động như một cầu nối giữa 2 tên miền. Vì vậy, bạn sẽ gửi một yêu cầu AJAX đến kịch bản phía máy chủ của bạn mà lần lượt sẽ gọi dịch vụ web từ xa và trả về kết quả.

+0

cảm ơn bạn, vì vậy tôi đã sai lầm khi nghĩ rằng việc sử dụng SOAP với một khách hàng JS đầy đủ là một công trình xung quanh. Và có, tôi đã thử nghiệm mã tôi đặt ở trên, nó hoạt động khi được sử dụng từ cùng một máy chủ. BTW tôi cũng sẽ cần rằng các máy chủ khác có thể làm như vậy ... tôi sẽ có một cái nhìn theo cách bạn đề nghị. – user1340802

1

mã bên dưới hoạt động tốt. có thể nó có thể giúp bạn.

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

Điều đó không hoạt động – Mukus

+0

Sẽ không hoạt động .. –

8

Làm thế nào về điều này? https://github.com/doedje/jquery.soap

Có vẻ dễ dàng. Có thể nó sẽ giúp được bạn.

Ví dụ:

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

sẽ dẫn đến

<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

với tệp svc như https: // localhost: 1303/BbsService.svc cách chúng tôi có thể sử dụng? – arslanaybars

Các vấn đề liên quan