I am trying to implement ngx-soap package here in my Angular 4 application. I am using Node server and tried to create soap client and server file using this package here and successfully implemented with the help I have got from here. Now I would to make the client in frontend. I have defined the body and but not sure whether that is correct.
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));
}
}
In Node, I have serverfile where I am trying to access the query from arangodb and once I run my client file .,I am able to print out the result envelope in console.I would like to do the same but from 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);
Since I am new to working with backend services. I couldn't solve the basic errors. I am not getting the proper hang of it.Like how to approach it ?
ERROR
First In order to solve..,I have re-edited the code which I have updated in the question. Then there was errors for cross-origin. That can be solved either by adding plugins or headers to the code.
Then I have used the function below.