我正在尝试使用 soap 模块在 node.js 中集成一个 soap api。当我点击 API 时,我得到的错误是"WSDL 或包含的意外根元素"。以下是 soap API 的详细信息:
POST /calculator.asmx HTTP/1.1
Host: www.dneonline.com
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<Add xmlns="http://tempuri.org/">
<intA>int</intA>
<intB>int</intB>
</Add>
</soap12:Body>
</soap12:Envelope>
我编写的节点js代码如下:
var soap = require('soap');
var url = 'http://www.dneonline.com/calculator.asmx';
var add = function(req, res){
var args = {intA: 1,intB: 2};
var soapHeader = ''//xml string for header
soap.createClient(url, function(err, client) {
if(err){
res.status(400).send(err)
} else {
client.Add(args, function(err, result) {
console.log('result ',result);
res.send(result)
});
}
});
}
exports.add = add
我认为此错误来自肥皂模块。那么我在请求中是否缺少某些内容,或者应该设置哪些标头以使nodejs代码正常工作?
尝试将网址更改为 http://www.dneonline.com/calculator.asmx?WSDL
例如
var url = 'http://www.dneonline.com/calculator.asmx?WSDL';
我已经创建了一个对下面 add 方法的简单调用:
var soap = require('soap');
var url = 'http://www.dneonline.com/calculator.asmx?WSDL';
var args = {intA: 1,intB: 2};
console.log('Creating client..');
soap.createClient(url, function(err, client) {
if(err){
console.log('Error creating client: ', err);
} else {
console.log('Client created..');
client.Add(args, function(err, result) {
console.log('result ',result);
});
}
});
我得到以下结果:
Creating client..
Client created..
result { AddResult: 3 }