Search code examples
javascriptandroidibm-mobilefirstworklight-adaptersworklight-server

How to perform a XMLHttpRequest on a Worklight Server Adapter?


Here is my adapter code

var fetchCommand = WL.Server.createSQLStatement("select * from basis_customers WHERE ID = ?");
function fetchData(param) {
      var xmlhttp = new XMLHttpRequest();
      xmlhttp.open('POST', 'http://ectest556ws1:9081/teamworks/webservices/POCODM/MobilityWS.tws', true);
      // build SOAP request
      var sr = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:mob="http://POCODM/MobilityWS.tws">'+
'<soapenv:Header/>'+
'<soapenv:Body>'+
'<mob:startProcess>'+
   '<mob:nombre>1111</mob:nombre>'+
   '<mob:cedula>2222</mob:cedula>'+
'</mob:startProcess>'+
'</soapenv:Body>'+
'</soapenv:Envelope>' ;

      xmlhttp.onreadystatechange = function () {
          if (xmlhttp.readyState == 4) {
              jq(xmlhttp.responseText).find("startProcessResponse").each(function () {
                var el = jq(this);
                alert(el.find("outputMessage").text());
              });
          }
      };

      // Send the POST request
      xmlhttp.setRequestHeader('Content-Type', 'text/xml');
      xmlhttp.send(sr);
    return WL.Server.invokeSQLStatement({
        preparedStatement : fetchCommand,
        parameters : [param]
    });
}

My SOAP WS works fine, but i dont understand how to call an XMLHttpRequest instance on my Worklight adapter. but when I call it I get this error :

Procedure invocation error. Ecma Error: ReferenceError: "XMLHttpRequest" is not defined. (C%3A%5CUsers%5Csasahoo%5Cworkspace%5Cback%5Cadapters%5CPerson/Person-impl.js#33) worklight.js:4556
WL.Logger.__log worklight.js:4556
PUBLIC_API.(anonymous function) worklight.js:4643
onInvokeProcedureSuccess worklight.js:7112
window.WLJSX.Ajax.WLRequest.WLJSX.Class.create.onSuccess worklight.js:3225
window.WLJSX.Ajax.WLRequest.WLJSX.Class.create.onWlSuccess worklight.js:3197
(anonymous function) worklight.js:947
window.WLJSX.Ajax.Request.WLJSX.Class.create.respondToReadyState worklight.js:1156
window.WLJSX.Ajax.Request.WLJSX.Class.create.onStateChange worklight.js:1094
(anonymous function)

Solution

  • You are trying to use Browser-style code in the Worklight Adapater environment, instead you need to use WL.Server capabilities.

    You will need two adapters: a SQL adapter and an HTTP adapter. You can call from one to the other

     SQLAdapter ---> HttpAdapter ---SOAP Reuqest---->  Your service
    

    Start by getting the Http Adapter going. Create an HTTP adapter, in its XML file specify your domain and port, that is ectest556ws1 and 9081. In the adapter implementation file, the .js, use

        var input = {
        method : 'get',
        returnedContentType : 'xml',
        path : "teamworks/webservices/POCODM/MobilityWS.tws",
    
    };
    
    
    return WL.Server.invokeHttp(input);
    

    Note that this is a synchronous call, you don't use callback methods. You can test this using the invokeProcedure capability in Studio.

    Then in you Database Adapter you can call across to the HTTP adapter:

      var invocationData = {
            adapter : "myHttpAdapter",
            procedure : "myCallSoapMethod",
            parameters : [ may, be, some, parameters ]
        };
    
    var soapResult =  WL.Server.invokeProcedure(invocationData);