使用Javascript的最简单的SOAP示例是什么?
为了尽可能有用,答案应该是:
具有功能性(换句话说就是实际工作) 发送至少一个可以在代码的其他地方设置的参数 处理至少一个可以在代码的其他地方读取的结果值 适用于大多数现代浏览器版本 在不使用外部库的情况下,尽可能地清晰和简短
使用Javascript的最简单的SOAP示例是什么?
为了尽可能有用,答案应该是:
具有功能性(换句话说就是实际工作) 发送至少一个可以在代码的其他地方设置的参数 处理至少一个可以在代码的其他地方读取的结果值 适用于大多数现代浏览器版本 在不使用外部库的情况下,尽可能地清晰和简短
当前回答
这不能直接用JavaScript完成,除非web服务与您的页面在同一个域中。编辑:在2008年和IE<10中,这不能用javascript直接完成,除非服务与你的页面在同一个域上。
如果web服务在另一个域中[并且您必须支持IE<10],那么您将不得不在您自己的域中使用代理页面来检索结果并将结果返回给您。如果你不需要旧的IE支持,那么你需要在你的服务中添加CORS支持。在这两种情况下,您都应该使用类似timyates建议的库,因为您不希望自己解析结果。
如果web服务在您自己的域中,那么不要使用SOAP。这样做没有很好的理由。如果web服务在您自己的域中,那么修改它,使它能够返回JSON,从而避免处理SOAP带来的所有麻烦。
简单的回答是:不要用javascript发出SOAP请求。使用web服务从另一个域请求数据,然后在服务器端解析结果,并以js友好的形式返回。
其他回答
这不能直接用JavaScript完成,除非web服务与您的页面在同一个域中。编辑:在2008年和IE<10中,这不能用javascript直接完成,除非服务与你的页面在同一个域上。
如果web服务在另一个域中[并且您必须支持IE<10],那么您将不得不在您自己的域中使用代理页面来检索结果并将结果返回给您。如果你不需要旧的IE支持,那么你需要在你的服务中添加CORS支持。在这两种情况下,您都应该使用类似timyates建议的库,因为您不希望自己解析结果。
如果web服务在您自己的域中,那么不要使用SOAP。这样做没有很好的理由。如果web服务在您自己的域中,那么修改它,使它能够返回JSON,从而避免处理SOAP带来的所有麻烦。
简单的回答是:不要用javascript发出SOAP请求。使用web服务从另一个域请求数据,然后在服务器端解析结果,并以js友好的形式返回。
有人试过吗?https://github.com/doedje/jquery.soap
似乎很容易实现。
例子:
$.soap({
url: 'http://my.server.com/soapservices/',
method: 'helloWorld',
data: {
name: 'Remy Blom',
msg: 'Hi!'
},
success: function (soapResponse) {
// do stuff with soapResponse
// if you want to have the response as JSON use soapResponse.toJSON();
// or soapResponse.toString() to get XML string
// or soapResponse.toXML() to get XML DOM
},
error: function (SOAPResponse) {
// show error
}
});
会导致
<soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<helloWorld>
<name>Remy Blom</name>
<msg>Hi!</msg>
</helloWorld>
</soap:Body>
</soap:Envelope>
基于XMLHttpRequest的Angularjs $http wrap。只要在头内容设置下面的代码就可以了。
"Content-Type": "text/xml; charset=utf-8"
例如:
function callSoap(){
var url = "http://www.webservicex.com/stockquote.asmx";
var soapXml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://www.webserviceX.NET/\"> "+
"<soapenv:Header/> "+
"<soapenv:Body> "+
"<web:GetQuote> "+
"<web:symbol></web:symbol> "+
"</web:GetQuote> "+
"</soapenv:Body> "+
"</soapenv:Envelope> ";
return $http({
url: url,
method: "POST",
data: soapXml,
headers: {
"Content-Type": "text/xml; charset=utf-8"
}
})
.then(callSoapComplete)
.catch(function(message){
return message;
});
function callSoapComplete(data, status, headers, config) {
// Convert to JSON Ojbect from xml
// var x2js = new X2JS();
// var str2json = x2js.xml_str2json(data.data);
// return str2json;
return data.data;
}
}
最简单的例子包括:
Getting user input. Composing XML SOAP message similar to this <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> POSTing message to webservice url using XHR Parsing webservice's XML SOAP response similar to this <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> Presenting results to user.
但是如果没有外部JavaScript库,就会有很多麻烦。
这里有一些很棒的例子(以及一个现成的JavaScript SOAP客户端!) http://plugins.jquery.com/soap/
检查自述文件,并注意同源浏览器限制。