我正在尝试在Angular 4应用程序中实现ngx-soap包here。我正在使用Node服务器并尝试使用此包here创建soap客户端和服务器文件,并使用我从这里获得的帮助成功实现。现在我想把客户端放在前端。我已经定义了身体,但不确定这是否正确。
import {Component, OnInit} from '@angular/core';
import {SOAPService, Client} from 'ngx-soap';
import {Http} from '@angular/http';
@Component({
selector: 'app-root',
templateUrl: './app.component.html'
})
export class AppComponent implements OnInit {
jsonResponse: any;
xmlResponse: string;
loading: boolean;
message: string;
private client: Client;
constructor(private http: Http, private soap: SOAPService) {}
ngOnInit() {
this.http.get('/assets/check_username.wsdl').subscribe(response => {
if (response && response.text()) {
this.soap.createClient(response.text()).then((client: Client) => {
this.client = client;
});
}
});
}
client_wsdl() {
this.clear();
this.loading = true;
let body = {
userName:"TEST_USER"
};
this.client.operation('checkUserName', body)
.then(operation => {
if (operation.error) {
console.log('Operation error', operation.error);
return;
}
let url = operation.url.replace("http://www.examples.com/", "/CheckUserName");
this.http.post(url, operation.xml, {
headers: operation.headers
}).subscribe(
response => {
this.xmlResponse = response.text();
this.jsonResponse = this.client.parseResponseBody(response.text());
try {
this.message = this.jsonResponse.Body.CheckUserNameResponse;
} catch (error) {}
this.loading = false;
},
err => {
console.log("Error calling ws", err);
this.loading = false;
}
);
})
.catch(err => console.log('Error', err));
}
}
在Node中,我有serverfile,我试图从arangodb访问查询,一旦我运行我的客户端文件。,我能够在console中打印出结果信封。我想做同样的但是从Angular 4。
server.js
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);
因为我刚开始使用后端服务。我无法解决基本错误。我没有得到正确的解决方案。如何接近它?
错误
答案 0 :(得分:0)
首先为了解决..,我重新编辑了我在问题中更新过的代码。然后是交叉起源的错误。这可以通过向代码添加插件或标头来解决。
然后我使用了以下功能。
function dbQuery() {
var response = 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',
});
console.log("response is "+ response);
return response;
}
//usage inside another async function
function main2() {
var result2 = dbQuery();
return result2.then(function(test){
console.log("test is "+JSON.stringify(test._result));
var IP = test._result.filter(Boolean);
console.log("Ip is "+IP);
});
}
main2();