代码部分:
/**
* Suds: A Lightweight JavaScript SOAP Client 一个轻量级的SOAP客户端
* Copyright: 2009 Kevin Whinnery (http://www.kevinwhinnery.com) 版权:xxx
* License: http://www.apache.org/licenses/LICENSE-2.0.html
* Source: http://github.com/kwhinnery/Suds 源码地址
*/
function SudsClient(_options) {
//A generic extend function - thanks MooTools //定义默认变量,修改变量
function extend(original, extended) { //传入原件和扩展数据
extended = extended || {}; //是否拥有对象,木有则创建对象
for (var key in extended) { //遍历对象
if (extended.hasOwnProperty(key)) { //是否拥有此方法
original[key] = extended[key]; //将extended自身的方法添加到原件中
}
}
return original; //返回原件
}
//Check if an object is an array //查看一个对象是否为数组
function isArray(obj) {
return Object.prototype.toString.call(obj) == '[object Array]'; //获取对象类型
}
//Grab an XMLHTTPRequest Object //创建一个XMLHTTPRequest对象
function getXHR() {
var xhr;
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
}
else {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
return xhr; //有对象了
}
//Parse a string and create an XML DOM object //通过解析string创建一个XMLDOM对象
function xmlDomFromString(_xml) { //传进来一个xml格式的string
var xmlDoc = null;
if (window.DOMParser) { //判断浏览器支持
parser = new DOMParser();
xmlDoc = parser.parseFromString(_xml,"text/xml"); //格式成xmlDoc
}
else {
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = "false"; //关闭异步
xmlDoc.loadXML(_xml); //加载
}
return xmlDoc; //返回
}
// Convert a JavaScript object to an XML string - takes either an //将对象(最好是JSON)为XML string
function convertToXml(_obj) {
var xml = '';
if (isArray(_obj)) { //先看一下对象是否为数组
for (var i = 0; i < _obj.length; i++) {
xml += convertToXml(_obj[i]);
}
} else {
//For now assuming we either have an array or an object graph //假设我们拥有数组或对象任意一个
for (var key in _obj) {
xml += '<'+key+'>';
if (isArray(_obj[key]) || (typeof _obj[key] == 'object' && _obj[key] != null)) {
xml += convertToXml(_obj[key]);
}
else {
xml += _obj[key];
}
xml += '</'+key+'>';
}
}
return xml; //方法就是遍历整个_obj,再单独遍历单个,将key作为节点,值为值
}
// Client Configuration //客户端配置
var config = extend({
endpoint:'http://localhost', //这些都是默认配置,在不修改的情况下使用
targetNamespace: 'http://localhost', //目标命名空间
envelopeBegin: '<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:ns0="PLACEHOLDER" 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/">',
headerBegin: '<soap:Header>',
headerNode:'head',
headerEnd: '</soap:Header>',
bodyBegin:'<soap:Body>',
envelopeEnd: '</soap:Body></soap:Envelope>',
timeout: 5000
},_options); //_options参数传入需要修改的部分
// Invoke a web service
this.invoke = function(_soapAction,_body,_callback,_error,_header) { //对象调用方法//不需要的直接不传
//Build request body
var body = _body; //body应该是一个对象样式,转成xml格式
var header = _header; //设置header
//Allow straight string input for XML body - if not, build from object
if (typeof body !== 'string') { //传入的参数判断
body = '<'+_soapAction+' xmlns="'+config.targetNamespace+'">';
body += convertToXml(_body); //配置格式
body += '</'+_soapAction+'>';
}//最终得到xml
var ebegin = config.envelopeBegin;
config.envelopeBegin = ebegin.replace('PLACEHOLDER', config.targetNamespace);
//Build Soapaction header - if no trailing slash in namespace, need to splice one in for soap action
var soapAction = ''; //这段是将_soapAction添加到url中,判定是否有'/'在最后
if (config.targetNamespace.lastIndexOf('/') != config.targetNamespace.length - 1) {
soapAction = config.targetNamespace+'/'+_soapAction;
}
else {
soapAction = config.targetNamespace+_soapAction;
}
//POST xml 到 服务终结点
var xhr = getXHR(); //创建对象
xhr.onload = function() { //回调函数执行
_callback.call(this, xmlDomFromString(this.responseText)); //将值转成XMLDOM格式
};
xhr.onerror = function() {
_error.call(); //回调错误代码
}
xhr.setTimeout(config.timeout); //延迟执行时间
var sendXML = '';
if(!header) { //参数_header暂时木有用到,设置另外的xml doc
sendXML = config.envelopeBegin+body+config.envelopeEnd; //发送到值
} else {
//Allow straight string input for XML body - if not, build from object
if (typeof header !== 'string') { //设置需要的sendXML
header = '<'+_soapAction+' xmlns="'+config.targetNamespace+'">';
header += convertToXml(_header);
header += '</'+_soapAction+'>';
}
sendXML = config.envelopeBegin+config.headerBegin+header+config.headerEnd+config.bodyBegin+body+config.envelopeEnd;
}
xhr.open('POST',config.endpoint); //open方法
xhr.setRequestHeader('Content-Type', 'text/xml'); //请求头类型
xhr.setRequestHeader('Content-Length', sendXML.length); //设置长度,节省资源?
xhr.setRequestHeader('SOAPAction', soapAction); //设置SOAPAction
if (config.authorization !== undefined) { //授权
xhr.setRequestHeader('Authorization', 'Basic ' + config.authorization);
}
xhr.send(sendXML); //发送出去
};
}
* Suds: A Lightweight JavaScript SOAP Client 一个轻量级的SOAP客户端
* Copyright: 2009 Kevin Whinnery (http://www.kevinwhinnery.com) 版权:xxx
* License: http://www.apache.org/licenses/LICENSE-2.0.html
* Source: http://github.com/kwhinnery/Suds 源码地址
*/
function SudsClient(_options) {
//A generic extend function - thanks MooTools //定义默认变量,修改变量
function extend(original, extended) { //传入原件和扩展数据
extended = extended || {}; //是否拥有对象,木有则创建对象
for (var key in extended) { //遍历对象
if (extended.hasOwnProperty(key)) { //是否拥有此方法
original[key] = extended[key]; //将extended自身的方法添加到原件中
}
}
return original; //返回原件
}
//Check if an object is an array //查看一个对象是否为数组
function isArray(obj) {
return Object.prototype.toString.call(obj) == '[object Array]'; //获取对象类型
}
//Grab an XMLHTTPRequest Object //创建一个XMLHTTPRequest对象
function getXHR() {
var xhr;
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
}
else {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
return xhr; //有对象了
}
//Parse a string and create an XML DOM object //通过解析string创建一个XMLDOM对象
function xmlDomFromString(_xml) { //传进来一个xml格式的string
var xmlDoc = null;
if (window.DOMParser) { //判断浏览器支持
parser = new DOMParser();
xmlDoc = parser.parseFromString(_xml,"text/xml"); //格式成xmlDoc
}
else {
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = "false"; //关闭异步
xmlDoc.loadXML(_xml); //加载
}
return xmlDoc; //返回
}
// Convert a JavaScript object to an XML string - takes either an //将对象(最好是JSON)为XML string
function convertToXml(_obj) {
var xml = '';
if (isArray(_obj)) { //先看一下对象是否为数组
for (var i = 0; i < _obj.length; i++) {
xml += convertToXml(_obj[i]);
}
} else {
//For now assuming we either have an array or an object graph //假设我们拥有数组或对象任意一个
for (var key in _obj) {
xml += '<'+key+'>';
if (isArray(_obj[key]) || (typeof _obj[key] == 'object' && _obj[key] != null)) {
xml += convertToXml(_obj[key]);
}
else {
xml += _obj[key];
}
xml += '</'+key+'>';
}
}
return xml; //方法就是遍历整个_obj,再单独遍历单个,将key作为节点,值为值
}
// Client Configuration //客户端配置
var config = extend({
endpoint:'http://localhost', //这些都是默认配置,在不修改的情况下使用
targetNamespace: 'http://localhost', //目标命名空间
envelopeBegin: '<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:ns0="PLACEHOLDER" 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/">',
headerBegin: '<soap:Header>',
headerNode:'head',
headerEnd: '</soap:Header>',
bodyBegin:'<soap:Body>',
envelopeEnd: '</soap:Body></soap:Envelope>',
timeout: 5000
},_options); //_options参数传入需要修改的部分
// Invoke a web service
this.invoke = function(_soapAction,_body,_callback,_error,_header) { //对象调用方法//不需要的直接不传
//Build request body
var body = _body; //body应该是一个对象样式,转成xml格式
var header = _header; //设置header
//Allow straight string input for XML body - if not, build from object
if (typeof body !== 'string') { //传入的参数判断
body = '<'+_soapAction+' xmlns="'+config.targetNamespace+'">';
body += convertToXml(_body); //配置格式
body += '</'+_soapAction+'>';
}//最终得到xml
var ebegin = config.envelopeBegin;
config.envelopeBegin = ebegin.replace('PLACEHOLDER', config.targetNamespace);
//Build Soapaction header - if no trailing slash in namespace, need to splice one in for soap action
var soapAction = ''; //这段是将_soapAction添加到url中,判定是否有'/'在最后
if (config.targetNamespace.lastIndexOf('/') != config.targetNamespace.length - 1) {
soapAction = config.targetNamespace+'/'+_soapAction;
}
else {
soapAction = config.targetNamespace+_soapAction;
}
//POST xml 到 服务终结点
var xhr = getXHR(); //创建对象
xhr.onload = function() { //回调函数执行
_callback.call(this, xmlDomFromString(this.responseText)); //将值转成XMLDOM格式
};
xhr.onerror = function() {
_error.call(); //回调错误代码
}
xhr.setTimeout(config.timeout); //延迟执行时间
var sendXML = '';
if(!header) { //参数_header暂时木有用到,设置另外的xml doc
sendXML = config.envelopeBegin+body+config.envelopeEnd; //发送到值
} else {
//Allow straight string input for XML body - if not, build from object
if (typeof header !== 'string') { //设置需要的sendXML
header = '<'+_soapAction+' xmlns="'+config.targetNamespace+'">';
header += convertToXml(_header);
header += '</'+_soapAction+'>';
}
sendXML = config.envelopeBegin+config.headerBegin+header+config.headerEnd+config.bodyBegin+body+config.envelopeEnd;
}
xhr.open('POST',config.endpoint); //open方法
xhr.setRequestHeader('Content-Type', 'text/xml'); //请求头类型
xhr.setRequestHeader('Content-Length', sendXML.length); //设置长度,节省资源?
xhr.setRequestHeader('SOAPAction', soapAction); //设置SOAPAction
if (config.authorization !== undefined) { //授权
xhr.setRequestHeader('Authorization', 'Basic ' + config.authorization);
}
xhr.send(sendXML); //发送出去
};
}
使用部分:
var value = '';
var suds = new SudsClient({
endpoint : url
});
try {
suds.invoke('', '', function(xmlDoc) {
var results = xmlDoc.documentElement.getElementsByTagName('EmementName');
if(results && results.length > 0) {
value = results.item(0).text;
}
});
} catch(e) {
Ti.API.error('Error: ' + e);
}
var suds = new SudsClient({
endpoint : url
});
try {
suds.invoke('', '', function(xmlDoc) {
var results = xmlDoc.documentElement.getElementsByTagName('EmementName');
if(results && results.length > 0) {
value = results.item(0).text;
}
});
} catch(e) {
Ti.API.error('Error: ' + e);
}
此处使用了GET方法,去掉了很多方法,表示可以使用!
新人初学js,有错误请指出,谢谢!