简体   繁体   English

jquery 和 random.org “访问控制允许来源不允许”

[英]jquery and random.org “is not allowed by Access-Control-Allow-Origin”

I am having a problem requesting a random number from random.org using jQuery.我在使用 jQuery 从 random.org 请求随机数时遇到问题。 When I'm using a static page and the following javascript, I don't have any issues getting random numbers.当我使用 static 页面和以下 javascript 时,获取随机数没有任何问题。 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."但是,我在 Heroku 上托管了一个 Sinatra 应用程序(此外,当使用 Thin 在生产中本地运行我的应用程序时)我得到“访问控制允许来源不允许(网站)”。

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?想法?

Well, on a quick check, random.org doesn't return any CORS headers.好吧,快速检查一下,random.org 没有返回任何CORS 标头。 This explains, why you are not allowed to request it via Javascript from your website.这解释了为什么不允许您从您的网站通过 Javascript 请求它。

Hav you loaded your static file, where it works, locally, ie, via file:/// protocol?您是否加载了 static 文件,它在本地工作,即通过file:///协议? There are other security measurements on duty, and browsers usually allow cross-domain AJAX.还有其他值班的安全措施,浏览器通常允许跨域 AJAX。

Honestly I don't get how you could do that in any way.老实说,我不明白你怎么能以任何方式做到这一点。 Because you shouldn't be able to do it.因为你不应该这样做。 Access-Control-Allow-Origin is an XSS protection which blocks cross domain requests and is part of most client side scripting engines (eg Flash). Access-Control-Allow-Origin 是一种 XSS 保护,可阻止跨域请求,并且是大多数客户端脚本引擎(例如 Flash)的一部分。 Use the following to random number generation:使用以下方法生成随机数:

Math.random()

Use jQuery YQL plugin to get result from this page.使用 jQuery YQL 插件从此页面获取结果。

It bypasses origin check by using Yahoo service to get content of needed page.它通过使用 Yahoo 服务绕过来源检查来获取所需页面的内容。

http://plugins.jquery.com/project/jquery-yql-plugin http://plugins.jquery.com/project/jquery-yql-plugin

But it is very complicated way.但这是非常复杂的方式。 Why don't you use Math.random()?为什么不使用 Math.random()?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM