我正在尝试使用 JavaScript 使用Web 服务( http://www.webservicex.net/CurrencyConvertor.asmx?wsdl ):
<script type="text/javascript">
function soap() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.open('GET', 'http://www.webservicex.net/CurrencyConvertor.asmx?wsdl',true);
var sr =
'<?xml version="1.0" encoding="utf-8"?>' +
'<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://www.webserviceX.NET/">'+
'<soapenv: Header/>'+
'<soapenv: Body>'+
'<web:ConversionRate>'+
'<web:FromCurrency>EUR</web:FromCurrency>'+
'<web:ToCurrency>TND</web:ToCurrency>'+
'</web:ConversionRate>'+
'</soapenv: Body>'+
'</soapenv:Envelope>';
xmlhttp.onreadystatechange = function (e) {
alert('onready');
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200) {
alert('done use firebug to see response');
}
}
if (xmlhttp.readyState == 0) {
alert('open() has not been called yet');
}
if (xmlhttp.readyState == 1) {
alert('send() has not been called yet');
}
if (xmlhttp.readyState == 2) {
alert('send() has been called, ...');
}
if (xmlhttp.readyState == 3) {
alert('Interactive - Downloading');
}
else
alert('Consuming Error '+e.message);
}
// Send the POST request
xmlhttp.setRequestHeader("SOAPAction", "http://www.webservicex.net/CurrencyConvertor/");
xmlhttp.setRequestHeader('Content-Type', 'text/xml');
xmlhttp.send(sr);
alert("Hi WS");
// send request
// ...
}
</script>
</head>
<body>
<form>
<input type="button" value="WS" onclick="soap();">
</form>
</form>
在执行过程中,我收到了onreadystatechange()
函数中提到的不同消息(甚至是错误消息!),但我不知道如何获得 Web 服务返回的答案(我尝试了这一行alert(xmlhttp.responseXML);
但徒劳无功),我不知道知道我是否做得对以及如何显示 Web 服务回复。