Previously on mustbebuilt we looked at cross domain calls using ‘vanilla’ Javascript. For completeness here are the same examples but this time using jQuery

Cross Domain Call with ‘crossdomain.xml’ in place

In this example Facebook provide the all important cross domain file.

$.getJSON("https://graph.facebook.com/search?q=sheffield", function(myData){
	var noPost = myData.data.length;
	var msg = "";
	for(var i=0; i<noPost;i++){
		msg += myData.data[i].message;
		msg += "<br>";	
	}
	$('#resp').html(msg);
});

View the Demo.

Cross Domain Call with using JSON-P

In this example notice that because we use an anonymous function the callback function is simply left as callback=?. Remove that part of the query string and you’ll get a cross domain error.

$.getJSON("http://local.yahooapis.com/LocalSearchService/V3/localSearch?appid=YahooDemo&query=pizza&zip=90210&output=json&callback=?", function(yahooData){
	var noPizzaPlaces = yahooData.ResultSet.Result.length;
	var msg = "";
	for(var i=0; i<noPizzaPlaces;i++){
		msg += yahooData.ResultSet.Result[i].Title;
		msg += "<br>";	
	}
	$('#resp').html(msg);
});

View the Demo.

Leave a Comment