I found this free currency exchange API http://openexchangerates.org/api/latest.json. This URL will return the exchange rates for about 150 currencies in JSON format. I have created an html web resource in CRM2011 to retrieve the exchange rates using JQuery AJAX call. Here is the code. I have also added a reference to JQuery web resource to this html web resource.
When we run this web resource, we will receive the following error message.
How to make a cross domain callsSo how can we get around this problem? There are few solutions to this problem. I will discuss the following 2 options.
- XDomainRequest (Cross Domain Request)
Using JSONPJSONP stands for JSON with padding but it has nothing to do with JSON. This is an old technique of using <script> tag to load external resources. JSONP dynamically creates a <script> tag to get the data instead of using XMLHttpRequest object. The <script> tag will load whatever is returned from the URL specified as its “src” attribute. The other cool feature of JSONP calls is Callback function.
For example look at the following URL
http://openexchangerates.org/api/latest.json. If we use this URL in <script> tag it will attached the response to “src” tag. But if we use http://openexchangerates.org/api/latest.json?callback=parseRequest, the response will be wrapped in a parseRequest method. We can use this method to process the response.
I have created an html web resource using this technique. Here is the code.
When we open the html web resource, it will display the following message.
In my next blog I will explain how to make JSONP calls using JQuery and AJAX and also how to use XDomainRequest.