我问了一个关于从GitHub中包含资源的问题,答案是使用原始链接:
https://raw.github.com/username/repository/branch/file.js
我正在尝试使用以下内容包含脚本:
但是我收到以下错误:
拒绝执行' https://raw.github.com/username/repo/master/src/file.js '中的脚本,因为它的MIME类型('text/plain')不可执行,并且启用了严格的MIME类型检查.
有没有其他方法可以解决此错误?
我不是在制作中使用它而是用于演示页面:
project-repository-from-github ?? master ? ?? src ? ? ?? my-jQuery-plugin.js ? ?? README.md ?? gh-pages ?? css ? ?? style.css ?? index.html
在index.html页面中,我想拥有my-jQuery-plugin.js的最新版本.所以,我会将原始URL包含在脚本中.
我该如何修复错误?
是的,Github 在2013年4月改变了这个:
我们
X-Content-Type-Options: nosniff
在2011年将标题添加到我们的原始URL响应中,这是打击热链接的第一步.这具有强制浏览器根据Content-Type
标题处理内容的效果.这意味着当我们设置Content-Type: text/plain
文件的原始视图时,浏览器将拒绝将该文件视为JavaScript或CSS.
但是由于http://combinatronics.com/,我们可以包含GH脚本.唯一的变化是从raw.github.com
变为combinatronics.com
:
<script type="text/javascript" src="https://combinatronics.com/username/repo/master/src/file.js" ></script>
该项目由Github托管,是开源的.
是的,@ Lix是正确的.这些文件不是从Github提供的,而是来自combinatronics.
我找到的另一个解决方法是代替:
<script type="text/javascript" src="https://combinatronics.com/username/repo/master/src/file.js" ></script>
你可以使用$.getScript
jQuery函数:
<script> $.getScript("https://combinatronics.com/username/repo/master/src/file.js", function () { /* do something when loaded */ }); </script>