我正在尝试编写我的第一个 javascript 调用名为“toto”的 Web 服务。
我在代码中提供了一个名为 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>