我认为您在创建客户端对象时出错。我建议您检查soap中的err对象。createClient调用,例如。
var options = {};
soap.createClient(url, options, function(err, client) {
if (err) {
console.error("An error has occurred creating SOAP client: " , err);
} else {
// Log a description of the services the server offers.
var description = client.describe();
console.log("Client description:" , description);
// Go on and call the method.
var method = client['check_username'];
method(requestArgs, function(err, result, envelope, soapHeader) {
console.log('Response Envelope: \n' + envelope);
console.log('Result: \n' + JSON.stringify(result));
}
}
});
记录客户端。说明非常有用,您可以看到处理请求的服务器对象的结构。
此外,客户端是否指向正确的URL?您正在服务器上的端口8000上侦听,客户端是否指向此端口?
在服务器端,我会连接记录器,这可以告诉你发生了什么,例如,你收到了什么信封,收到了什么请求等等。
var soapServer = soap.listen(server, '/wsdl', myService, xml);
soapServer.log = function(type, data) {
// type is 'received' or 'replied'
console.log('Type: ' + type + ' data: ' + data);
};
下面是一个完整的示例:
服务器
var soap = require('strong-soap').soap;
var http = require('http');
var myService = {
CheckUserName_Service: {
CheckUserName_Port: {
checkUserName: function(args, soapCallback) {
console.log('checkUserName: Entering function..');
db.query(aqlQuery`
LET startVertex = (FOR doc IN spec
FILTER doc.serial_no == '"123456abcde"'
LIMIT 2
RETURN doc
)[0]
FOR v IN 1 ANY startVertex belongs_to
RETURN v.ip`,
{
bindVar1: 'value',
bindVar2: 'value',
}
).then(function(response) {
console.log(`Retrieved documents.`, response._result);
soapCallback(JSON.stringify(response._result));
})
.catch(function(error) {
console.error('Error getting document', error);
soapCallback('Error getting document' + error.message);
});
}
}
}
};
var xml = require('fs').readFileSync('check_username.wsdl', 'utf8');
var server = http.createServer(function(request,response) {
response.end("404: Not Found: " + request.url);
});
var port = 8000;
server.listen(port);
var soapServer = soap.listen(server, '/test', myService, xml);
soapServer.log = function(type, data) {
console.log('Type: ' + type + ' data: ' + data);
};
console.log('SOAP service listening on port ' + port);
客户
"use strict";
var soap = require('strong-soap').soap;
var url = 'http://localhost:8000/test?wsdl';
var options = { endpoint: 'http://localhost:8000/test'};
var requestArgs = { userName: "TEST_USER" };
soap.createClient(url, options, function(err, client) {
if (err) {
console.error("An error has occurred creating SOAP client: " , err);
} else {
var description = client.describe();
console.log("Client description:" , description);
var method = client.checkUserName;
method(requestArgs, function(err, result, envelope, soapHeader) {
//response envelope
console.log('Response Envelope: \n' + envelope);
//'result' is the response body
console.log('Result: \n' + JSON.stringify(result));
});
}
});
WSDL
<definitions name = "CheckUserNameService"
targetNamespace = "http://www.examples.com/wsdl/CheckUserNameService.wsdl"
xmlns = "http://schemas.xmlsoap.org/wsdl/"
xmlns:soap = "http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tns = "http://www.examples.com/wsdl/CheckUserNameService.wsdl"
xmlns:xsd = "http://www.w3.org/2001/XMLSchema">
<message name = "CheckUserNameRequest">
<part name = "userName" type = "xsd:string"/>
</message>
<message name = "CheckUserNameResponse">
<part name = "status" type = "xsd:string"/>
</message>
<portType name = "CheckUserName_PortType">
<operation name = "checkUserName">
<input message = "tns:CheckUserNameRequest"/>
<output message = "tns:CheckUserNameResponse"/>
</operation>
</portType>
<binding name = "CheckUserName_Binding" type = "tns:CheckUserName_PortType">
<soap:binding style = "rpc"
transport = "http://schemas.xmlsoap.org/soap/http"/>
<operation name = "checkUserName">
<soap:operation soapAction = "checkUserName"/>
<input>
<soap:body encodingStyle = "http://schemas.xmlsoap.org/soap/encoding/" namespace = "urn:examples:CheckUserNameService" use = "encoded"/>
</input>
<output>
<soap:body encodingStyle = "http://schemas.xmlsoap.org/soap/encoding/" namespace = "urn:examples:CheckUserNameService" use = "encoded"/>
</output>
</operation>
</binding>
<service name = "CheckUserName_Service">
<documentation>WSDL File for CheckUserNameService</documentation>
<port binding = "tns:CheckUserName_Binding" name = "CheckUserName_Port">
<soap:address
location = "http://www.examples.com/CheckUserName/" />
</port>
</service>
</definitions>
WSDL应该位于名为“check\u username”的文件中。与服务器位于同一目录中。