Consuming web service returns empty value


#1

Hello everyone,

I had a look i the code provided here to consume a local web service but I get an empty returned value, I don’t see why.

In my case, I want to call the LocalServiceLoginToken function, the value returned is a string element, I put this code in the ts file of my Home page :

function soapRequest(){
   let TokenCode='My token value';
   let return_value=	'<?xml version="1.0" encoding="UTF-8"?>'+
   					'<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'+
   					'<SOAP-ENV:Header/>'+
   					'<SOAP-ENV:Body>'+
   					'<ns1:LocalServiceLoginToken xmlns:ns1="http://www.sage.com/fr/line1000/Sage1000Authentication">'+
   					'<ns1:token>'+TokenCode+'</ns1:token>'+
   					'</ns1:LocalServiceLoginToken>'+
   					'</SOAP-ENV:Body>'+
   					'</SOAP-ENV:Envelope>';
   
   return return_value;
}

function readBody(xhr) {
   let data;
   if (!xhr.responseType || xhr.responseType === "text") {
       data = xhr.responseText;
   } else if (xhr.responseType === "document") {
       data = xhr.responseXML;
   } else {
       data = xhr.response;
   }
   return data;
}	

function Autentication_get_sessionId() { 
   //as I don't know the right url address of my web service, I put 2 
   //url addresses, so to test, I simply switched these 2 variables :
   let url_autentication2='http://win-9u040a2kpbv:83/adelerh/server/soap.l1000?wsdl=Sage1000Authentication';
   let url_autentication1='http://win-9u040a2kpbv:83/adelerh/server/soap.l1000';
   
   let xmlhttp = new XMLHttpRequest();
   xmlhttp.open('POST', url_autentication1, true);

   xmlhttp.onreadystatechange = function () {
   if (xmlhttp.readyState == 4) {
                  alert(readBody(xmlhttp));
               }
           }
   // Send the POST request
   xmlhttp.setRequestHeader('Content-Type', 'text/xml');
   xmlhttp.send(soapRequest());
}

To run my application, I simply call the Autentication_get_sessionId function but the returned value is empty.

Thanks for your help


#2

I think it might require an authorization header as part of the request.
Have you tried it via POSTMAN.


#3

You mean I would have to edit the soap header to execute the web services functions?
If so, the authentication needs nothing special, I need to run it to get a connection id and I need to use it to edit each function (before I was coding with Ionic, I was coding with Webdev and it’s exactly what I was doing, at the exception that I wasn’t sending soap envelopes, I was directly running the functions and I dynamically editing the soap header)


#4

Can you do it via POSTMAN or any online tool to check whether you are able to hit your WSDL correctly or not. Also, what are the console errors you are getting


#5

I don’t manage to consume the web service via Postman, I have the application via google chrome.

I edited my code, here you have mine.
By reading this message I found here, but I have a new error which says that “$” cannot be found.

Am I right to consume the web service via AJAX?

EDIT: by reading this article, shall I use promises instead?


#6

AJAX is done via jquery.( You don’t need jquery because angular already has the same)
I see that your POSTMAN request contains an token, are you sure you are passing the same in your ionic code.

For soap request, i normally do $http.post which returns a promise by default.

Check what errors you get inside an error callback function as per below example.

$http.post(url, data, config)
            .success(function (data, status, headers, config) {
                $scope.PostDataResponse = data;
            })
            .error(function (data, status, header, config) {
                $scope.ResponseDetails = "Data: " + data +
                    "<hr />status: " + status +
                    "<hr />headers: " + header +
                    "<hr />config: " + config;
            });

#7

I have indeed a token as I’m calling the authentication web service, by sending a token code, I have in return a session Id as string value, I need it to call every web service method.
If the token code isn’t correct, the authentication web service returns an error which says that the token can’t establish the connection.

So, about my new code, I changed the parameters to the constructor, I have a second one and it’s like this :

constructor(public navCtrl: NavController, public $http: ng.IHttpService)

I used this site to find how to “declare” a $http object, but I have a new error which says that “ng” can’t be found, “ng” isn’t supposed to be an angular directive? so, there should have no trouble to call it, right?
Moreover, to call the $http.post method, can I send json parameter, like this :

let query= {
	method: 'POST',
	url: "http://win-9u040a2kpbv:83/adelerh/server/soap.l1000?wsdl=Sage1000Authentication",
	data: soapRequest(),
	datatype: "text/xml; charset=utf-8",
	headers: {
	  'Content-Type': undefined
	}
   }

$http.post(query)
            .success(function (query) {
                $scope.PostDataResponse = query["data"];
            })
            .error(function (query) {
                $scope.ResponseDetails = "Data: " + query["data"]+
                    "<hr />status: " + query["status"] +
                    "<hr />headers: " + query["header"] +
                    "<hr />config: " + query["config"];
            });

#8

For ng can’t be found, you can ask that on ionic v2 forum as i have never seen that error.

No idea, why you are sending Content Type as Undefined.
Run the above code and see what errors you are getting on error callback function