Created
September 19, 2011 09:58
-
-
Save chico/1226245 to your computer and use it in GitHub Desktop.
Cross Domain Ajax
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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