javascript - 在 Node-soap (node.js) 中发送带有数组的请求

标签 javascript node.js node-soap

我正在使用 nodejs 和 node-soap 与网络服务通信。但我似乎无法获得将参数传递给服务的正确语法。

文档说我需要发送一个包含字段 uuid 及其值的数组。

这是我从网络服务所有者那里得到的示例 Php 代码

$uuid = "xxxx";

    $param = array("uuid"=>new SoapVar($uuid,
    XSD_STRING,
    "string", "http://www.w3.org/2001/XMLSchema")
    )

这是我在 Node 服务器中使用的代码

 function getSoapResponse()
{
    var soap = require('soap');
      var url = 'http://live.pagoagil.net/soapserver?wsdl';
      var auth = [{'uuid': 'XXXXXXXXX'}];

      soap.createClient(url, function(err, client) {
      client.ListaBancosPSE(auth, function(err, result) 
      {
          console.log(result);
          console.log(err);
      });
  });

有了这个我得到了错误的 xml 错误

var auth = [{'uuid': 'XXXXXXXXX'}];

var auth = [["uuid",key1],XSD_STRING,"string","http://www.w3.org/2001/XMLSchema"];

然后我得到响应“用户 ID 为空”(uuid)

 var auth = {'uuid': 'XXXXXXXXX'};

有什么建议吗?

最佳答案

最后使用this中的内容回答并修改 soap-node 模块中的代码我能够获得我需要的代码。

我需要这样的东西:

      <auth xsi:type="ns2:Map">
        <item>
          <key xsi:type="xsd:string">uuid</key>
          <value xsi:type="xsd:string">{XXXXXX}</value>
        </item>
      </auth>

所以我用它来创建参数:

var arrayToSend= 
{auth :
    [  
        {   'attributes' : {'xsi:type':"ns2:Map"}, 
            'item':  
                [
                    {'key' :
                        {'attributes' : 
                            { 'xsi:type': 'xsd:string'},
                            $value: 'uuid'
                        }
                    },
                    {'value' :
                        {'attributes' : 
                            { 'xsi:type': 'xsd:string'},
                            $value: uuid
                        }
                    }
                ]
        }   

    ]
};

然后像这样发送:

soap.createClient(url, myFunction);


    function myFunction(err, client) 
    {
      client.ListaBancosPSE(arrayToSend,function(err, result) 
      {
          console.log('\n' + result);
      });
    }

然后棘手的部分是 modyfing wsd.js 所以它不会在我每次使用和数组时添加额外的标签。我转到第 1584 行并为此更改了 if:

    if (Array.isArray(obj)) 
  {
      var arrayAttr = self.processAttributes(obj[0]),
          correctOuterNamespace = parentNamespace || ns; //using the parent namespace if given
      parts.push(['<', correctOuterNamespace, name, arrayAttr, xmlnsAttrib, '>'].join(''));
    for (var i = 0, item; item = obj[i]; i++) 
    {

      parts.push(self.objectToXML(item, name, namespace, xmlns, false, null, parameterTypeObject, ancXmlns));
    }
      parts.push(['</', correctOuterNamespace, name, '>'].join(''));
  } 

基本上现在它不会在每个迭代中推送打开和关闭标签,而是只在整个循环之前和之后推送。

我还需要为消息的 xlmns 添加定义。 Client.js:186

xml = "<soap:Envelope " +
    "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" " +
    'xmlns:xsd="http://www.w3.org/2001/XMLSchema"' +
    'xmlns:ns2="http://xml.apache.org/xml-soap"' +
    "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +

希望这对使用这个库的人和处于这种情况的人有所帮助。

关于javascript - 在 Node-soap (node.js) 中发送带有数组的请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28865128/

相关文章:

javascript - ReactJS 是否为服务器端呈现的应用程序两次呈现应用程序? (重复代码)

mongodb - 验证我是否通过 Mongoose 连接到我的 MongoDB?

javascript - 遍历数组以使用 d3 追加圆圈

javascript - 如何创建动态 if 条件

javascript - 返回的 socket.io 对象在 client.js 的范围之外不可用

javascript - 在 Node.js 中处理来自 SOAP Web 服务调用的字符串响应的最佳方法

node.js - TypeError : obj. hasOwnProperty 在调用 Graphql mutation 时不是一个函数

javascript - 如何在输入文本后显示按钮

javascript - MySQL 查询由于某种原因未执行

Node.js Soap 附件文件/cid href