Categories
WordPress

How To Consume Web Services With Sencha Touch

It’s possibile to consume a web service with Sencha Touch? Yes, with the Ajax.request service. Sencha Touch seem to have no tools to easily consume a web service, creating a soap envelope and getting back the XML response. But there is a way to send that envelope (manually created) with Ajax.request. Here an example of code created today by a colleague dealing with this problem.

The URLs are only example, in our case we were calling a web service exposed from Windows IIS Server.

Ext.Ajax.request({
   url: 'http://ws.domain.com/iphone.asmx',
   callbackKey: 'callback',
   scope: 'this',
   method: 'POST',
   headers: {'Content-Type': 'application/soap+xml; charset=utf-8; action="http://ws.domain.com/iphonenews"'},
   success: function(response) {
      // OK
   },
   failure: function(response) {
      // KO
   },
   xmlData: '<?xml version="1.0" encoding="utf-8"?>\n<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:csp="http://ws.domain.com/"><soap:Header/><soap:Body><csp:iphonenews><csp:lang>ita</csp:lang></csp:iphonenews></soap:Body></soap:Envelope>'
 });

Note the call is SOAP 1.2 with the action passed on Content-Type header (you can alternatively use the SOAPAction header) and  the xmlData key which contain the full envelope (don’t miss the \n after the XML version declaration or remove it, many times it is not required).

The response is an XMLHttpRequest, contains the XML and you can get it with response.responseText or response.responseXML.

What to say… good luck!

 

 

2 replies on “How To Consume Web Services With Sencha Touch”

Leave a Reply