Javascript XMLHttpRequest:从web服务检索输出
我正试图编写我的第一个javascript调用名为“toto”的web服务 我在代码中提供了一个名为data的xml,并希望从web服务接收一个输出xml(进行centigrad-farenheit转换) 我在Firefox中本地执行代码(我从IE或Chrome中获得相同的结果)。web服务位于vm映像webservicevmimage上 当我从soapui调用web服务时,它会工作Javascript XMLHttpRequest:从web服务检索输出,javascript,xml,web-services,Javascript,Xml,Web Services,我正试图编写我的第一个javascript调用名为“toto”的web服务 我在代码中提供了一个名为data的xml,并希望从web服务接收一个输出xml(进行centigrad-farenheit转换) 我在Firefox中本地执行代码(我从IE或Chrome中获得相同的结果)。web服务位于vm映像webservicevmimage上 当我从soapui调用web服务时,它会工作 <script> function makeRequest() {
<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>
函数makeRequest(){
var-httpRequest=false;
var strMethodName=‘toto/转换温度’;
var strSoapContent=“”;
var Temp=document.getElementById('inputTemp')。值;
var Unit=document.getElementById('inputUnit')。值;
var soapActionhttp://tempuri.org/toto/converting_temperature';
var url='1〕http://webservicevmimage:8080/toto&NoCache' ;
var data=“”
+ ' '
+ ''
+ ''
+ ''
+ ' '
+ ' '
+ ' '
+ ''
+ ''
+''温度+''
+''+单位''
+ ''
+ ''
+ ''
+ ' '
+ ''
+ ''
+ ''
+ '';
如果(window.XMLHttpRequest){//Mozilla,Safari,。。。
httpRequest=新的XMLHttpRequest();
if(httpRequest.overrideMimeType){
httpRequest.overrideMimeType('text/xml');
}
}
如果(window.ActiveXObject){//IE
试一试{
httpRequest=新的ActiveXObject(“Msxml2.XMLHTTP”);
}
捕获(e){
试一试{
httpRequest=新的ActiveXObject(“Microsoft.XMLHTTP”);
}
捕获(e){}
}
}
如果(!httpRequest){
警报(“放弃:(不可能的撤销请求une实例XMLHTTP”);
返回false;
}
httpRequest.onreadystatechange=函数(){alertContents(httpRequest);};
httpRequest.open('POST',url,true);
setRequestHeader('Content-Type','application/soap+xml');
httpRequest.setRequestHeader('SOAPAction',SOAPAction);
警报('XML文件:\n'+数据);
httpRequest.send(数据);
var parser=新的DOMParser();
var xmlDoc=parser.parseFromString(httpRequest.responseText(),“application/xml”);
警报(hxmlDoc);
/*
警报($(httpRequest.responseXML.find('CALCULATEDTEMPERATURE'));
var answer=doc.getElementsByTagName(“计算温度”).值;
警惕(回答)*/
函数alertContents(xmlhttp){
if(xmlhttp.readyState==4&&(xmlhttp.status==200 | | xmlhttp.status==0))
{
警报(xmlhttp.responseXML.xml);
}
否则{
警报(“连接错误:就绪状态=“+xmlhttp.readyState+”状态=“+xmlhttp.Status”);
}
}
/*
readystate的值不同
0--未初始化
1--装载
2--已加载(但未接收数据
3--交互--接收部分数据
4——已完成(收到所有数据)
*/
}
在开发网络元素时,在本地运行代码(文件:\\\\)是不好的做法。例如,Windows会抛出安全错误
尝试将代码放在远程服务器上,甚至放在本地服务器上(wamp/lamp/etc)
PS:为什么要重新发明轮子?有数千个图书馆可供网络通话使用。以下是我最喜欢的两个: