Recently we had to consume web services which were exposed by a third party provider for one of our web application based on recently released  mobile web application framework called Sencha. The web services response was in complex XML format.

In Sencha framework following options are available to consume cross-domain web services.

  1. Ext.util.JSONP – Provides functionality to make cross-domain requests with JSONP (JSON with Padding). (http://en.wikipedia.org/wiki/JSON#JSONP).

     

  2. Ext.data.ScriptTagProxy – An implementation of Ext.data.DataProxy that reads a data object from a URL which may be in a domain other than the originating domain of the running page.

Out of the above two options, ScriptTagProxy would serve my purpose better but for that to work, the <script> tag has to be handled at the server side. Since we were dealing with third party server and could not change the server side coding this option was ruled out. So to consume the web services we had the following approach. Here the server acts a proxy for your actual AJAX calls. This is called Cross Domain Proxy.

  1. Written small server side programs (servlets) and consumed the third party web services. Here the choice of server side programming is up to the comfortable level of the developer. I personally chose Java, since I could quickly setup the environment with excellent tools like Apache Tomcat server, Eclipse J2EE IDE etc.

     

  2. Also the XML output from the third party web services was converted to JSON (using JSON-lib, http://json-lib.sourceforge.net/ ) since JSON format is native to javascript and can be easily manipulated.

     

  3. Created an AJAX request from my web application which simply calls the relevant servlet which calls the corresponding third party web service.

     

  4. Get the JSON response from the servlet and manipulate it according to the application need.

This is one approach we could think of, probably if there is any other better approach, please let us know.

Advertisements