SOAP 是基于 XML 的简易协议,可使应用程序在 HTTP 之上进行信息交换。
项目开发中需要请求webservice服务,前端主要使用node.js 作为运行环境,因此可以使用soap进行请求。
使用SOAP请求webservice服务的流程如下:
1.进入项目目录,安装 soap 模块
> npm install soap --save-dev
安装后node_modules文件夹下
2.项目中引用
在main.js文件中 导入 Soap
import Soap from 'jquery.soap
Vue.use(Soap);
调用webservice
var soap = require('soap');
var url=“https://www.xxx.com/services/SimpleWS?wsdl/
使用soap访问webservice
let Base64 = require('js-base64').Base64;
var postData={}//请求的数据
let sendData = Base64.encode(postData); //该方法返回一个 base-64 编码的字符串
try {
$.soap({
url:url,
method: 'process',
data: { sendData },
headers: {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/javascript; charset=gbk'
},
contentType: 'text/xml;charset=gbk',
timeout: 60000,
success: function(soapResponse) {
let str = window.atob(soapResponse.content.children[0].textContent);
if (str && str != undefined) {
let result = str.substr(16, str.length);
let resultJson = JSON.parse(iconvLite.decode(result, 'gbk')); //json化gbk编码的数据 iconvLite为引入的解决中文乱码的js库
let respObj = resultJson;
respObj.respcode = '0000';
if (resultJson.ALInfoError.Sucess != 1) {
respObj.respcode = resultJson.ALInfoError.Code;
}
respObj.respmsg = resultJson.ALInfoError.Description;
calbak(respObj); //回调
} else {
let respObj = {
urls: apiUrl,
respcode: '4009',
respmsg: '未返回结果!'
};
console.log('接口错误', respObj);
calbak(respObj);
}
},
error: function(SOAPResponse) {
// show error
let errobj = {
respcode: '504',
respmsg: '链接ERP出错!'
};
calbak(errobj);
}
});
} catch (error) {
let errobj = {};
if (typeof error == 'string') {
console.log('服务响应为字符串');
errobj = {
respcode: '504',
respmsg: '服务器端未正确响应'
};
}
if (error) {
console.log('服务有问题!');
errobj = {
respcode: '999',
respmsg: '网络连接失败或接口调用出错'
};
}
if (error.respcode) {
errobj.respcode = error.respcode;
}
if (error.respmsg) {
errobj.respmsg = error.respmsg;
}
calbak(errobj);
}