2013-01-11 49 views
0

我试图写我的第一个JavaScript调用名为“toto”的Web服务。XMLHttpRequest:从Web服务检索输出

我在代码中提供了一个xml命名数据,并希望从web服务接收输出xml(进行转换centigrad-farenheit)。

我在Firefox本地执行代码(从IE或Chrome获得相同的结果)。 Web服务位于vm image 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> 

回答

0

运行本地代码(文件:\\\)是不好的做法,开发网络元素时。 Windows(例如)引发安全错误。

尝试把一个远程服务器上的代码,甚至本地服务器(WAMP /灯/等)

PS上:为什么要重新发明轮子?有数以千计的网络电话库可供使用。这里是我的两个最爱: