soap 简易协议 访问webservice

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);
  }

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值