2016-11-25 98 views
2

首先對我的英語感到抱歉,其次我有一個關於在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 

有人能幫我解決嗎? 謝謝並再次抱歉我的英語。

回答

3

我遇到過類似的問題。也許你試圖使用的SOAP Web服務有v1.2規範,它可能期望內容類型爲application/soap + xml而不是text/xml。爲了強制node-soap使用SOAP 1.2版本,您可以添加forceSoap12Headers:true之間的createClient()參數。

在一個側面說明,我不得不的的WS-Addressing標頭由於與To「」不能在接收器進行處理,該消息皁頭添加由於在EndpointDispatcher誤差的AddressFilter失配。

我編輯你的代碼如下:您創建客戶端代碼後client.setSecurity(new soap.BasicAuthSecurity('username', 'password'));

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

添加該代碼。它對我有用:

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