我正在使用 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/