Skip to content

Instantly share code, notes, and snippets.

@chico
Created September 19, 2011 09:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save chico/1226245 to your computer and use it in GitHub Desktop.
Save chico/1226245 to your computer and use it in GitHub Desktop.
Cross Domain Ajax
<html>
<head>
<script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
<head>
<body>
<script>
$(document).ready(function () {
// Cross domain ajax (uses YQL)
// See http://james.padolsey.com/javascript/cross-domain-requests-with-jquery/
crossDomainAjax = function(url, callback) {
$.ajax({
url: 'http://query.yahooapis.com/v1/public/yql?callback=?',
dataType: 'json',
data: {
q: 'select * from html where url="' + url + '" and xpath="*"',
format: 'xml'
},
success: function(data) {
$(data.results[0]).filter('p').each(function() {
callback($(this).text());
return;
});
}
});
}
// Example cross domain ajax request
crossDomainAjax(
'http://www.bbc.co.uk/iplayer/ion/episodedetail/episode/b014qryx/format/json',
function(data) {
var ionDetails = jQuery.parseJSON(data);
console.log(ionDetails.blocklist[0].brand_title);
console.log("http://www.bbc.co.uk" + ionDetails.blocklist[0].my_url);
}
);
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment