Monday, July 23, 2012

CRM2011 and Cross Domain calls Part 2

In my last blog, we discussed what is JSONP and how to make cross domain calls using JSONP? In this blog, I will share how to make cross domain calls using jQuery and Ajax. I will use the currency exchange and twitter APIs to make JSONP calls. As of version 1.5 jQuery supports JSONP calls.

How to make a JSONP call using jQuery

Here is the code. The code is using the twitter APIs. The code will retrieve the tweets by Donna Edward( CRM MVP) and display the text of the first tweet.

Run the code and we will received the similar to the following screen.

How to make a JSONP call using AJAX

Here is the code call the JSONP call using AJAX.

<HTML><HEAD><TITLE></TITLE>
<SCRIPT src="http://auntmsftv23/SandpitAmreek/WebResources/new_/jquery1.7.2.js"></SCRIPT>
<SCRIPT type=text/javascript>
function callTheJsonp()
{
debugger;
// the url of the script where we send the asynchronous call
var url = "http://openexchangerates.org/api/latest.json?callback=parseRequest";
$.ajax({
url: url,
dataType: "jsonp",
jsonpCallback: "parseRequest"
});
}
// this function should parse responses.. you can do anything you need..
// you can make it general so it would parse all the responses the page receives based on a data field
function parseRequest(data)
{
try // try to output this to the javascript console
{
alert("AUD: " + data.rates.AUD);}
catch(an_exception)
// alert for the users that don't have a javascript console
{
alert(data);
}
} </SCRIPT>
<META charset=utf-8></HEAD>
<BODY onload=callTheJsonp() contentEditable=true>Testing Cross Domain Calls</BODY></HTML>