Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery and random.org "is not allowed by Access-Control-Allow-Origin"

Tags:

jquery

sinatra

I am having a problem requesting a random number from random.org using jQuery. When I'm using a static page and the following javascript, I don't have any issues getting random numbers. However, I am hosting a Sinatra app on Heroku (also, when running my app locally in production using Thin) I get "(website) is not allowed by Access-Control-Allow-Origin."

function raffler(){

var rowCount = $('#winnerTable tr').length;

$('#winnerButton').click(function() {
    $.get("http://www.random.org/integers/?", {num: "1", min: "1", max: rowCount, col: "1", base: "10", format: "plain", rnd: "new"}, function(randNum) {
        var myNumber = randNum;
        $("#entry-" + randNum).addClass('winner');
    });
});

};

Thoughts?

like image 684
YuKagi Avatar asked Oct 10 '22 06:10

YuKagi


1 Answers

Well, on a quick check, random.org doesn't return any CORS headers. This explains, why you are not allowed to request it via Javascript from your website.

Hav you loaded your static file, where it works, locally, i.e., via file:/// protocol? There are other security measurements on duty, and browsers usually allow cross-domain AJAX.

like image 63
Boldewyn Avatar answered Oct 14 '22 05:10

Boldewyn