0

「toto」という名前の Web サービスを呼び出す最初の JavaScript を作成しようとしています。

コードで data という名前の xml を提供していますが、Web サービスから出力 xml を受け取りたいと考えています (centigrad-farenheit の変換を行っています)。

コードを Firefox でローカルに実行しています (IE または Chrome から同じ結果が得られます)。Web サービスは、vm イメージ webservicevmimage にあります。

SOAP UI から Web サービスを呼び出すと、機能します。

<script>

            function makeRequest() {
                var httpRequest = false;
                var strMethodName = 'toto/converting_temperature' ;
                var strSoapContent = "" ;   
                var Temp=document.getElementById('inputTemp').value;
                var Unit=document.getElementById('inputUnit').value;
                var soapAction = 'http://tempuri.org/toto/converting_temperature';
                var url = 'http://webservicevmimage:8080/toto&NoCache' ;
                var data = "<?xml version=\'1.0\' encoding=\'utf-8\'?>"
                + '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:toto="http://tempuri.org/toto"> '
                +   '<soapenv:Header/>'
                +   '<soapenv:Body>'
                +   '<toto:converting_temperature>'
                +   ' <toto:streams>'
                +   '   <toto:ws_in contentType="?">'
                +   '      <toto:Value>'
                + '<TABLE>'
                + '<INTABLE>'
                + '<temperature>' + Temp + '</temperature>'
                + '<Unit>' + Unit + '</Unit>'
                + '</INTABLE>'
                + '</TABLE>'
                + '</toto:Value>'
                + '   </toto:ws_in>'
                + '</toto:streams>'
                + '</toto:converting_temperature>'
                + '</soapenv:Body>'
                + '</soapenv:Envelope>';
                if (window.XMLHttpRequest) { // Mozilla, Safari,...
                    httpRequest = new XMLHttpRequest();
                    if (httpRequest.overrideMimeType) {
                        httpRequest.overrideMimeType('text/xml');
                    }
                }
                else if (window.ActiveXObject) { // IE
                    try {
                        httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
                    }
                    catch (e) {
                        try {
                            httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                        }
                        catch (e) {}
                    }
                }
                if (!httpRequest) {
                    alert('Abandon :( Impossible de créer une instance XMLHTTP');
                    return false;
                }

                httpRequest.onreadystatechange = function() { alertContents(httpRequest); };

                httpRequest.open('POST', url, true);                

                httpRequest.setRequestHeader('Content-Type', 'application/soap+xml');   
                httpRequest.setRequestHeader('SOAPAction', soapAction);



                alert('XML File:\n' + data);

                httpRequest.send(data);     

                var parser = new DOMParser();
                var xmlDoc = parser.parseFromString(httpRequest.responseText(), "application/xml"); 

                alert(hxmlDoc);
                /*
                alert($(httpRequest.responseXML).find('CALCULATEDTEMPERATURE'));

                var answer = doc.getElementsByTagName("CALCULATEDTEMPERATURE").value;

                alert (answer);*/


            function alertContents(xmlhttp) {
                if (xmlhttp.readyState == 4 && ( xmlhttp.status == 200 || xmlhttp.status == 0))
                 {
                            alert(xmlhttp.responseXML.xml);
                 }
                else {
                            alert("Connection Error: Ready State=" + xmlhttp.readyState + " Status=" +  xmlhttp.status);
                    }
                }
                /*
                Different value for readystate
                    0--Uninitialized
                    1--Loading
                    2--loaded(but data not recieved                 
                    3--Interactive--Some part of the data is recieved                   
                    4--Completed(all data recieved)
                */
            }

    </script>
4

1 に答える 1

0

ネットワーク要素を開発する場合、コードをローカル(file:\\\)で実行することはお勧めできません。たとえば、Windowsはセキュリティエラーをスローします。

コードをリモートサーバー、またはローカルサーバー(wamp / lamp / etc)に配置してみてください

PS:なぜ車輪の再発明をするのですか?利用可能なネットワーク呼び出し用のライブラリは何千もあります。これが私の2つのお気に入りです:

于 2013-01-11T15:38:40.277 に答える