Skip to content

Instantly share code, notes, and snippets.

@Gowiem
Last active December 24, 2015 10:59
Show Gist options
  • Save Gowiem/6788056 to your computer and use it in GitHub Desktop.
Save Gowiem/6788056 to your computer and use it in GitHub Desktop.
Why does jQuery#getJSON work outside of this closure?
// It seems my request is fine when its not wrapped in a closure,
// but blows up with CORS errors when inside...
// The following works fine with no issues
var videoUrl = "http://vimeo.com/api/v2/video/75738368.json?callback=?"
$.getJSON(videoUrl, function (data) {
console.log("Data: ", data)
});
Hist.VideoHandler = function() {
return {
getVimeoId: function(url) {
var regex = /vimeo.com\/(\d*)/i;
return url.match(regex)[1];
},
// The grabVimeoImage function fails after sending the request
// due to the following CORS errors:
// OPTIONS http://vimeo.com/api/v2/video/75738368.json?callback=jQuery1830285762949148193_1380678929998&_=1380678930532
// Origin http://localhost:5000 is not allowed by Access-Control-Allow-Origin.
//
// Halp.
grabVimeoImage: function($urlContainer, vimeoId) {
var videoUrl = "http://vimeo.com/api/v2/video/" + vimeoId + ".json?callback=?"
console.log("Before $.getJSON");
$.getJSON(videoUrl, function (data) {
console.log("Data: ", data)
});
},
updateVimeos: function($vimeos) {
var self = this;
$vimeos.each(function() {
var $this = $(this),
dataUrl = $this.data('url'),
vimeoId = self.getVimeoId(dataUrl);
self.grabVimeoImage($this, vimeoId);
});
},
init: function() {
var $vimeos = $('.vimeo_video');
console.log("vimeos: ", $vimeos);
if ($vimeos.length > 0) {
this.updateVimeos($vimeos);
}
}
}
}();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment