Nic*_*asZ 2 javascript node.js node-soap
我正在使用 Nodejs 和 Node-Soap 与 Web 服务进行通信。但我似乎无法获得将参数传递给服务的正确语法。
文档说我需要发送一个包含字段 uuid 及其值的数组。
这是我从网络服务所有者那里得到的 PHP 代码作为示例
$uuid = "xxxx";
$param = array("uuid"=>new SoapVar($uuid,
XSD_STRING,
"string", "http://www.w3.org/2001/XMLSchema")
)
Run Code Online (Sandbox Code Playgroud)
这是我在节点服务器中使用的代码
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);
});
});
Run Code Online (Sandbox Code Playgroud)
有了这个我得到了错误的 xml 错误
var auth = [{'uuid': 'XXXXXXXXX'}];
Run Code Online (Sandbox Code Playgroud)
或者
var auth = [["uuid",key1],XSD_STRING,"string","http://www.w3.org/2001/XMLSchema"];
Run Code Online (Sandbox Code Playgroud)
这样我得到的响应是“用户 ID 为空”(uuid)
var auth = {'uuid': 'XXXXXXXXX'};
Run Code Online (Sandbox Code Playgroud)
有什么建议么?
最后使用这个答案中的内容并修改soap-node模块中的代码我能够获得我需要的代码。
我需要这样的东西:
<auth xsi:type="ns2:Map">
<item>
<key xsi:type="xsd:string">uuid</key>
<value xsi:type="xsd:string">{XXXXXX}</value>
</item>
</auth>
Run Code Online (Sandbox Code Playgroud)
所以我用它来创建参数:
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
}
}
]
}
]
};
Run Code Online (Sandbox Code Playgroud)
并像这样发送:
soap.createClient(url, myFunction);
function myFunction(err, client)
{
client.ListaBancosPSE(arrayToSend,function(err, result)
{
console.log('\n' + result);
});
}
Run Code Online (Sandbox Code Playgroud)
然后棘手的部分是修改,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(''));
}
Run Code Online (Sandbox Code Playgroud)
基本上现在它不会在每个迭代中推送打开和关闭标签,而是仅在整个周期之前和之后推送。
我还需要添加消息的 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\" " +
Run Code Online (Sandbox Code Playgroud)
希望这对使用这个库并处于这种情况的人有所帮助。