这可能是一个简单的问题,但我无法理解。
我正在尝试从托管在我的网站上的Web应用程序中访问github API。这是代码要点:
<!DOCTYPE html>
<html>
<head>
<style>p { color:red; }</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function () {
$.ajax( {url :'https://api.github.com/repos/janesconference/kievIIPlugins/commits', dataType: "json", cache: false, success: function (data, textStatus, jqXHR)
{
var lastCommitSha = data[0].sha;
$("p").text("Last commit SHA: " + lastCommitSha);
}
});
});
</script>
</head>
<body>
<p>Ajax request not (yet) executed.</p>
</body>
</html>
如果我将浏览器指向我上传到我的Dropbox帐户的这个简单网页,一切都很好。
但是,如果我将浏览器指向我上传到我的网站的这个简单网页,我会得到臭名昭著的Access-Control-Allow-Origin
异常。XMLHttpRequest cannot load https://api.github.com/repos/janesconference/kievIIPlugins/commits?_=1360060011783. Origin http://bitterspring.net is not allowed by Access-Control-Allow-Origin.
那么,问题是:
- 为什么在 Dropbox 上可以工作?
- 我明白使用 CORS 可以甚至在网站上工作。这涉及将
Access-Control-Allow-Origin: *.github.com
放在我的 Apache 配置中或类似的东西。但是,正如从 en.wiki 引用的那样,
但这可能不适用于安全是一个问题的情况
- 是否有一种方法可以在不更改 Apache 配置的情况下执行此操作?可能我不能触摸我的托管站点的 Apache 配置,并且始终存在安全问题。正确的做法是什么?