无法解析响应SOAP Node.js

时间:2016-11-25 10:50:01

标签: javascript node.js soap

首先抱歉我的英语,其次我有一个关于在Node.js中使用SOAP的问题。我是node.js的初学者,我需要帮助。这是我的功能:

var soap = require('soap');

var url = 'http://SOMETHING?wsdl';

var args = {
    accountId: 'xxxxx',
    userName: 'xxxxx',
    password: 'xxxxxx',
    targetNSAlias: 'tns',
    targetNamespace: 'http://api.ilient.com/'
};

soap.createClient(url, function(err, client) {
    if(err) throw err;    
            client.login(args,function(err, result, raw, soapHeader){
            if(err) throw err;
            console.log(result);
    });
});

当我跑步时,我收到此错误:

Error: Cannot parse response
at /root/node_modules/soap/lib/client.js:321:21
at Request._callback (/root/node_modules/soap/lib/http.js:117:5)
at Request.self.callback (/root/node_modules/request/request.js:186:22)
at Request.emit (events.js:98:17)
at Request.<anonymous> (/root/node_modules/request/request.js:1081:10)
at Request.emit (events.js:95:17)
at IncomingMessage.<anonymous> (/root/node_modules/request/request.js:1001:12)
at IncomingMessage.g (events.js:180:16)
at IncomingMessage.emit (events.js:117:20)
at _stream_readable.js:944:16
有人可以帮我解决一下吗? 再次感谢和抱歉我的英语。

2 个答案:

答案 0 :(得分:3)

我遇到了类似的问题。您尝试使用的SOAP Web服务可能具有v1.2规范,并且可能期望内容类型为application / soap + xml而不是text / xml。为了强制node-soap使用SOAP 1.2版本,您可以在 createClient()参数中添加 forceSoap12Headers:true

在旁注中,我必须将 ws-addressing 标头添加到soap标头,因为带有To&#39;的消息&#39;由于EndpointDispatcher 错误导致AddressFilter不匹配,因此无法在接收方处理。

我按如下方式编辑了您的代码:

var soap = require('soap');
var url = 'http://SOMETHING?wsdl';
var args = {
    accountId: 'xxxxx',
    userName: 'xxxxx',
    password: 'xxxxxx',
    targetNSAlias: 'tns',
    targetNamespace: 'http://api.ilient.com/'
};

var soapOptions = {
    forceSoap12Headers: true
};

var soapHeaders = {
    'wsa:Action': 'http://tempuri.org/MyPortName/MyAction',
    'wsa:To': 'http://SOMETHING.svc'
};

soap.createClient(url, soapOptions, function(err, client) {
    if(err) throw err;  
    client.addSoapHeader(soapHeaders, '', 'wsa', 'http://www.w3.org/2005/08/addressing');  
    client.login(args, function(err, result, raw){
        if(err) throw err;
        console.log(result);
    });
});

答案 1 :(得分:0)

在创建客户端代码后添加此代码:client.setSecurity(new soap.BasicAuthSecurity('username', 'password'));。它对我有用:

var soap = require('soap');
var url = 'http://SOMETHING?wsdl';
soap.createClient(url, function(err, client) {
if(err) throw err;   
  client.setSecurity(new soap.BasicAuthSecurity('username', 'password'));
  client.login(args, function(err, result) {
  if(err) throw err;   
        console.log(result);
   });
});