2012-10-09 25 views
13

Javascriptから直接SOAP WebServiceを呼び出したいと思います。私は周りを見てきましたが、まだ何かを働かせることができません。私はSOAPエンベロップを構築する必要があると仮定しました(下記参照)。私もjQueryを使用します。Javascript/jQueryからSOAP WSを呼び出す方法

まず、SOAP Webサービスを別の場所に置くことができますか?つまり、クロスドメインの制限のような制限はありません。

また、SOAPサービスはLadonを使用して公開されており、デバッグの目的で公開されています.WSがsoapUIでうまく動作することを確認しています。

  • WSDLのURL:http://192.168.1.5/ws/MyWS/soap
  • のSOAPAction:http://192.168.1.5/ws/MyWS/soap/myOperation
//私の理解から、それはこの1つの
  • サービスエンドポイントにすることはできません

    私はエンドポイントまたはSOAPActionを使うべきだと思うが、うまくいかなかった。私はここで何かが恋しくなるかもしれないし、後でJavascriptが間違っていると確信できない。

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

    よろしく

    EDIT:答えを試してみて、検索を続けながら、私は今ここに

    は、コールをやって、私の実際のJS(コメント内のいくつかの質問があります)です=>Simplest SOAP exampleと読んでいますが、Prestaulは「ウェブサービスがあなたのページと同じドメインにない限り、これをストレートJavaScriptで行うことはできません」と言っています。だから、私は何か不可能なことをしようとしているのだろうか?それがうまくいかない理由ですか?

  • 答えて

    20

    ブラウザに組み込まれているsame origin policyの制限のため、クロスドメインのAJAXリクエストを送信することはできません。この作業を行うには、jQueryコードを含むHTMLページをWebサービス(http://192.168.1.5/ws/MyWS/)と同じドメインでホストする必要があります。

    サーバー上でJSONPを使用するという回避策がありますが、WebサービスがSOAPであるため、これは機能しません。

    ウェブサービスと同じドメインでjavascriptを移動できない場合、この作業を行う唯一の信頼できる方法は、javascriptコードと同じドメインでホストされるサーバーサイドスクリプトを構築することです。 2つのドメイン間のブリッジ。したがって、サーバー側のスクリプトにAJAXリクエストを送信し、リモート側のWebサービスを呼び出して結果を返します。

    +0

    になりますが、あなたに感謝ので、私は完全なJSクライアントとSOAPを使用すると、周りの仕事だったことを考えるのは間違っていました。そして、はい、上記のコードをテストしました。同じサーバーから使用すると動作します。 BTW私はまた、他のサーバーがそうすることができるようにする必要があります...私はあなたが提案する方法を見てみるつもりです。 – 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

    は、https:// localhost:1303/BbsService.svcのようなsvcファイルでどのように使用できますか? – arslanaybars

    関連する問題