最简单的SOAP示例
使用Javascript最简单的SOAP示例是什么?
为了尽可能有用,答案应该是:
- 具有功能性(换句话说,实际上有效)
- 发送至少一个可以在代码中其他位置设置的参数
- 处理至少一个可以在代码的其他位置读取的结果值
- 与大多数现代浏览器版本一起使用
- 在不使用外部库的情况下尽可能清晰明了
解决方案
最简单的示例包括:
- 获取用户输入。
- 编写与此类似的XML SOAP消息
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetInfoByZIP xmlns="http://www.webserviceX.NET">
<USZip>string</USZip>
</GetInfoByZIP>
</soap:Body>
</soap:Envelope>
- 使用XHR将消息发布到Web服务URL
- 解析Web服务的XML SOAP响应与此类似
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetInfoByZIPResponse xmlns="http://www.webserviceX.NET">
<GetInfoByZIPResult>
<NewDataSet xmlns="">
<Table>
<CITY>...</CITY>
<STATE>...</STATE>
<ZIP>...</ZIP>
<AREA_CODE>...</AREA_CODE>
<TIME_ZONE>...</TIME_ZONE>
</Table>
</NewDataSet>
</GetInfoByZIPResult>
</GetInfoByZIPResponse>
</soap:Body>
</soap:Envelope>
- 向用户展示结果。
但是,如果没有外部JavaScript库,就会遇到很多麻烦。
除非Web服务与页面位于同一域中,否则不能使用直接的JavaScript来完成。编辑:在2008年和IE <10中,除非服务与页面位于同一域中,否则无法使用纯正的javascript完成。
如果Web服务在另一个域上[并且必须支持IE <10],则必须在自己的域上使用一个代理页面,该代理页面将检索结果并将其返回给我们。如果不需要旧的IE支持,则需要在服务中添加CORS支持。无论哪种情况,都应该使用timyates建议的lib之类的东西,因为我们不想自己解析结果。
如果Web服务在我们自己的域中,则不要使用SOAP。没有充分的理由这样做。如果Web服务在我们自己的域中,则对其进行修改,以便它可以返回JSON,从而省去了处理SOAP带来的所有麻烦的麻烦。
简短的答案是:不要从javascript发出SOAP请求。使用Web服务从另一个域请求数据,如果这样做,则在服务器端解析结果并以js友好的形式返回它们。
托马斯:
JSON是前端使用的首选,因为它是javascript。因此,我们无需处理任何XML。因此,SOAP不使用库就很麻烦。有人提到SOAPClient,这是一个很好的库,我们从项目开始就使用它。但是,它有一些局限性,我们不得不重写其中的很大一部分。它以SOAPjs的形式发布,并支持将复杂对象传递到服务器,并包含一些示例代理代码以使用来自其他域的服务。
浏览器处理XMLHttpRequest的方式有很多奇怪之处,此JS代码可在所有浏览器中使用:
https://github.com/ilinsky/xmlhttprequest
此JS代码将XML转换为易于使用的JavaScript对象:
http://www.terracoder.com/index.php/xml-objectifier
上面的JS代码可以包含在页面中,以满足我们对外部库的要求。
var symbol = "MSFT";
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", "http://www.webservicex.net/stockquote.asmx?op=GetQuote",true);
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState == 4) {
alert(xmlhttp.responseText);
// http://www.terracoder.com convert XML to JSON
var json = XMLObjectifier.xmlToJSON(xmlhttp.responseXML);
var result = json.Body[0].GetQuoteResponse[0].GetQuoteResult[0].Text;
// Result text is escaped XML string, convert string to XML object then convert to JSON object
json = XMLObjectifier.xmlToJSON(XMLObjectifier.textToXML(result));
alert(symbol + ' Stock Quote: $' + json.Stock[0].Last[0].Text);
}
}
xmlhttp.setRequestHeader("SOAPAction", "http://www.webserviceX.NET/GetQuote");
xmlhttp.setRequestHeader("Content-Type", "text/xml");
var xml = '<?xml version="1.0" encoding="utf-8"?>' +
'<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' +
'xmlns:xsd="http://www.w3.org/2001/XMLSchema" ' +
'xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' +
'<soap:Body> ' +
'<GetQuote xmlns="http://www.webserviceX.NET/"> ' +
'<symbol>' + symbol + '</symbol> ' +
'</GetQuote> ' +
'</soap:Body> ' +
'</soap:Envelope>';
xmlhttp.send(xml);
// ...Include Google and Terracoder JS code here...
另外两个选择:
- JavaScript SOAP客户端:http://www.guru4.net/articoli/javascript-soap-client/zh/
- 从WSDL生成JavaScript:https://cwiki.apache.org/confluence/display/CXF20DOC/WSDL+to+Javascript
使用JavaScript轻松使用SOAP Web服务->清单B
function fncAddTwoIntegers(a, b)
{
varoXmlHttp = new XMLHttpRequest();
oXmlHttp.open("POST",
"http://localhost/Develop.NET/Home.Develop.WebServices/SimpleService.asmx'",
false);
oXmlHttp.setRequestHeader("Content-Type", "text/xml");
oXmlHttp.setRequestHeader("SOAPAction", "http://tempuri.org/AddTwoIntegers");
oXmlHttp.send(" \
<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' \
xmlns:xsd='http://www.w3.org/2001/XMLSchema' \
xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'> \
<soap:Body> \
<AddTwoIntegers xmlns='http://tempuri.org/'> \
<IntegerOne>" + a + "</IntegerOne> \
<IntegerTwo>" + b + "</IntegerTwo> \
</AddTwoIntegers> \
</soap:Body> \
</soap:Envelope> \
");
return oXmlHttp.responseXML.selectSingleNode("//AddTwoIntegersResult").text;
}
这可能无法满足所有要求,但实际上是回答问题的开始。 (我将XMLHttpRequest()切换为ActiveXObject(" MSXML2.XMLHTTP"))。

