尝试通过Node调用National Rail SOAP API时出现TypeError

时间:2015-02-08 20:56:31

标签: xml node.js soap typeerror node-soap

我正在尝试使用node-soap访问National Rail API。

文档在这里:https://lite.realtime.nationalrail.co.uk/OpenLDBWS/

我是SOAP的新手,所以这可能不是API的问题。我正在尝试使用我尝试的任何类型的错误...

  

TypeError:无法读取未定义的属性“Body”

以下是我尝试拨打电话的代码:

var soap = require('soap');
var url = 'https://lite.realtime.nationalrail.co.uk/OpenLDBWS/wsdl.aspx?ver=2014-02-20';
var soapHeader = '<com:AccessToken>MY_TOKEN</com:AccessToken>';

soap.createClient(url, function(err, client){

  var args = {
    numRows : 10,
    crs : 'LAN'
  };

  client.addSoapHeader(soapHeader);

  client.GetDepartureBoard(args, function(err, result){
    if(err){
      console.log('error!');
      throw err;
    }
    console.log(result);
  });  
});

使用node-soap我也可以获取请求信封,这是内容:

<soap:Envelope
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:tns="http://thalesgroup.com/RTTI/2014-02-20/ldb/"
    xmlns:tok="http://thalesgroup.com/RTTI/2013-11-28/Token/types"
    xmlns:ct="http://thalesgroup.com/RTTI/2007-10-10/ldb/commontypes"
    xmlns:ldbt="http://thalesgroup.com/RTTI/2012-01-13/ldb/types"
    xmlns:ldbt2="http://thalesgroup.com/RTTI/2014-02-20/ldb/types">
    <soap:Header>
        <com:AccessToken>MY_TOKEN</com:AccessToken>
    </soap:Header>
    <soap:Body>
        <tns:GetDepartureBoardRequest
            xmlns:tns="http://thalesgroup.com/RTTI/2014-02-20/ldb/"
            xmlns="http://thalesgroup.com/RTTI/2014-02-20/ldb/">
            <tns:numRows>10</tns:numRows>
            <tns:crs>LAN</tns:crs>
        </tns:GetDepartureBoardRequest>
    </soap:Body>
</soap:Envelope>

1 个答案:

答案 0 :(得分:2)

您的soap标头格式不正确,需要'&lt;'AccessToken'&gt;''&lt;'/ AccessToken'&gt;' 包裹着它

var args, soap, soapHeader, url;

soap = require('soap');

url = 'https://lite.realtime.nationalrail.co.uk/OpenLDBWS/wsdl.aspx?ver=2014-02-20';

soapHeader = '<AccessToken><TokenValue>Your access token</TokenValue></AccessToken>'

// or you can write it as an object
//soapHeader = {
//    AccessToken: {
//        TokenValue: "Your access token"
//    }
//};

args = {
    numRows: 10,
    crs: 'BSK'
};

soap.createClient(url, function(err, client) {
    client.addSoapHeader(soapHeader);
    return client.GetArrivalDepartureBoard(args, function(err, result) {
        return console.log(JSON.stringify(result));
    });
});