Titanium SOAP Call Problem TypeError: 'undefined' is not an object (evaluating 'suds.invoke')

You must Login before you can answer or comment on any questions.

Hi All! I want to call getPoems function from SOAP Call. my web service address is http://seerah.designers99.com/webservice/server.php?wsdl I have used code bellow.

Titanium.include('funs/suds.js');
 
//=========( SOAP Working )================
Ti.API.info('SOAP Working Start: ');
alert('Start SOAP Call: ');
try{
// var window = Ti.UI.currentWindow;
var window = Ti.UI.currentWindow;
var label = Ti.UI.createLabel({
    top: 10,
    left: 10,
    width: 'auto',
    height: 'auto',
    text: 'Fetching Poem Results from WebService'
});
 
window.add(label);
 
 
var url = "http://seerah.designers99.com/webservice/server.php?wsdl";
var callparams = {
    // FromCurrency: 'EUR',
    // ToCurrency: 'USD'
};
 
var suds = new SudsClient({
    endpoint: url,
    targetNamespace: 'http://seerah.designers99.com/webservice/'
});
Ti.API.info('End SOAP Working: ');
}
catch(e){
 
    Ti.API.info('Error: '+e);
}
 
Ti.API.info('SOAP Call Start: ');
try {
    suds.invoke('getPoems', callparams, function(xmlDoc) {
        var results = xmlDoc.documentElement.getElementsByTagName('getPoemsResult');
        if (results && results.length>0) {
            var result = results.item(0);
            label.text = 'Poem Results are  ' + results.item(0).text + ' <<<<<<<.';
        } else {
            label.text = 'Oops, could not determine result of SOAP call.';
        }
    });
    Ti.API.info('SOAP Call End: ');
} catch(e) {
 
    Ti.API.error('Error: ' + e);
}
alert('End SOAP Call');
 
 
//=========( End SOAP Working )============
Please Help me out to get Poems? I have placed the suds.js in 'funs/' directory I have checked getPoems() function on soapUI-3.0.1 Software which results fine but in titanium it is generating such errors.

2 Answers

Accepted Answer

hi,

try this

Titanium.include(Ti.Filesystem.resourcesDirectory + 'funs/suds.js');
 
var url = "http://seerah.designers99.com/webservice/server.php?wsdl";
var callparams = {
    // FromCurrency: 'EUR',
    // ToCurrency: 'USD'
};
 
var suds = new SudsClient({
    endpoint: url,
    targetNamespace: 'http://seerah.designers99.com/webservice/'
});
 
try{
.
.
.
.
.
.
 
}
catch(e){
 
}
 
try {
    suds.invoke('getPoems', callparams, function(xmlDoc) {
        var results = xmlDoc.documentElement.getElementsByTagName('getPoemsResult');
        if (results && results.length>0) {
            var result = results.item(0);
            label.text = 'Poem Results are  ' + results.item(0).text + ' <<<<<<<.';
        } else {
            label.text = 'Oops, could not determine result of SOAP call.';
        }
    });
    Ti.API.info('SOAP Call End: ');
} catch(e) {
 
    Ti.API.error('Error: ' + e);
}

— answered 1 year ago by Mitul Bhalia
answer permalink
6 Comments
  • Hi Mital, thank you. it works but data is not fetched yet. SOAP call successful. :)

    — commented 1 year ago by Abdus Sattar

  • it's Mitul not Mital ;)

    you should try to alert your responseText

    — commented 1 year ago by Mitul Bhalia

  • all is well. now everything is working fine. Data and SOAP call. I was getting by incorrect tag name 'getPoemsResult'

    xmlDoc.documentElement.getElementsByTagName('getPoemsResult');
    Now I am getting fine with correct Tag Name 'poem'
    xmlDoc.documentElement.getElementsByTagName('poem');
    works fine now :)

    — commented 1 year ago by Abdus Sattar

  • Show 3 more comments

Hi Abdul,

Declare this outside your try-catch block

var suds;

— answered 1 year ago by Nitin Chavda
answer permalink
4 Comments
  • I have declared outside try catch block but still getting same error :(

    — commented 1 year ago by Abdus Sattar

  • Okey, can you tell me what is inside file.

    Titanium.include('funs/suds.js');

    — commented 1 year ago by Nitin Chavda

  • Hi Nitin, "funs/suds.js" contents

    /**
    * Suds: A Lightweight JavaScript SOAP Client
    * Copyright: 2009 Kevin Whinnery (http://www.kevinwhinnery.com)
    * License: http://www.apache.org/licenses/LICENSE-2.0.html
    * Source: http://github.com/kwhinnery/Suds
    */
    function SudsClient(_options) {
      function isBrowserEnvironment() {
        try {
          if (window && window.navigator) {
            return true;
          } else {
            return false;
          }
        } catch(e) {
          return false;
        }
      }
     
      function isAppceleratorTitanium() {
        try {
          if (Titanium) {
            return true;
          } else {
            return false;
          }
        } catch(e) {
          return false;
        }
      }
     
      //A generic extend function - thanks MooTools
      function extend(original, extended) {
        for (var key in (extended || {})) {
          if (original.hasOwnProperty(key)) {
            original[key] = extended[key];
          }
        }
        return original;
      }
     
      //Check if an object is an array
      function isArray(obj) {
        return Object.prototype.toString.call(obj) == '[object Array]';
      }
     
      //Grab an XMLHTTPRequest Object
      function getXHR() {
        var xhr;
        if (isBrowserEnvironment()) {
          if (window.XMLHttpRequest) {
            xhr = new XMLHttpRequest();
          }
          else {
            xhr = new ActiveXObject("Microsoft.XMLHTTP");
          }
        }
        else if (isAppceleratorTitanium()) {
          xhr = Titanium.Network.createHTTPClient();
        }
        return xhr;
      }
     
      //Parse a string and create an XML DOM object
      function xmlDomFromString(_xml) {
        var xmlDoc = null;
        if (isBrowserEnvironment()) {
          if (window.DOMParser) {
            parser = new DOMParser();
            xmlDoc = parser.parseFromString(_xml,"text/xml");
          }
          else {
            xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
            xmlDoc.async = "false";
            xmlDoc.loadXML(_xml); 
          }
        }
        else if (isAppceleratorTitanium()) {
          xmlDoc = Titanium.XML.parseString(_xml);
        }
        return xmlDoc;
      }
     
      // Convert a JavaScript object to an XML string - takes either an
      function convertToXml(_obj, namespacePrefix) {
        var xml = '';
        if (isArray(_obj)) {
          for (var i = 0; i < _obj.length; i++) {
            xml += convertToXml(_obj[i], namespacePrefix);
          }
        } else {
          //For now assuming we either have an array or an object graph
          for (var key in _obj) {
            if (namespacePrefix && namespacePrefix.length) {
              xml += '<' + namespacePrefix + ':' + key + '>';
            } else {
              xml += '<'+key+'>';
            }
            if (isArray(_obj[key]) || (typeof _obj[key] == 'object' && _obj[key] != null)) {
              xml += convertToXml(_obj[key]);
            }
            else {
              xml += _obj[key];
            }
            if (namespacePrefix && namespacePrefix.length) {
              xml += '</' + namespacePrefix + ':' + key + '>';
            } else {
              xml += '</'+key+'>';
            }
          }
        }
        return xml;
      }
     
      // Client Configuration
      var config = extend({
        endpoint:'http://localhost',
        targetNamespace: 'http://localhost',
        envelopeBegin: '<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:ns0="PLACEHOLDER" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body>',
        envelopeEnd: '</soap:Body></soap:Envelope>'
      },_options);
     
      // Invoke a web service
      this.invoke = function(_soapAction,_body,_callback) {    
        //Build request body 
        var body = _body;
     
        //Allow straight string input for XML body - if not, build from object
        if (typeof body !== 'string') {
          body = '<ns0:'+_soapAction+'>';
          body += convertToXml(_body, 'ns0');
          body += '</ns0:'+_soapAction+'>';
        }
     
        var ebegin = config.envelopeBegin;
        config.envelopeBegin = ebegin.replace('PLACEHOLDER', config.targetNamespace);
     
        //Build Soapaction header - if no trailing slash in namespace, need to splice one in for soap action
        var soapAction = '';
        if (config.targetNamespace.lastIndexOf('/') != config.targetNamespace.length - 1) {
          soapAction = config.targetNamespace+'/'+_soapAction;
        }
        else {
          soapAction = config.targetNamespace+_soapAction;
        }
     
        //POST XML document to service endpoint
        var xhr = getXHR();
        xhr.onload = function() {
          _callback.call(this, xmlDomFromString(this.responseText));
        };
        xhr.open('POST',config.endpoint);
            xhr.setRequestHeader('Content-Type', 'text/xml');
            xhr.setRequestHeader('SOAPAction', soapAction);
            xhr.send(config.envelopeBegin+body+config.envelopeEnd);
      };
    }

    — commented 1 year ago by Abdus Sattar

  • Show 1 more comment

Your Answer

Think you can help? Login to answer this question!