問題描述
我正在嘗試從我的 GitHub 存儲庫中讀取一些源代碼(C 語言),以便在我的網頁中顯示為文本.我可以通過 https://raw.github.com 以原始模式訪問代碼.
I'm trying to read some source code (C language) from my GitHub repository to be shown as text in my webpage. I can access the code in raw mode through https://raw.github.com.
我正在使用 jQuery GET 函數來讀取數據,但它不起作用.問題與 XMLHttpRequest 和 Access-Control-Allow-Origin 有關,但是盡管我在 stackoverflow 上發現了一些相關問題(XmlHttpRequest 錯誤:Access-Control-Allow-Origin 不允許使用 Origin null)它對我不起作用.我什么都試過了.
I'm using jQuery GET function to read the data but it doesn't work. Problem is related with XMLHttpRequest and Access-Control-Allow-Origin but, despite I found some related question on stackoverflow (XmlHttpRequest error: Origin null is not allowed by Access-Control-Allow-Origin) it didn't work for me. I tried everything.
我的 jQuery 代碼:
My jQuery code:
<script type="text/javascript">
$(document).ready(function() {
var url = 'https://raw.github.com/raysan5/raylib/master/examples/ex01_basic_window.c';
$.get(url, function(data) {
$('#code').text(data);
}, 'text');
});
</script>
請問,有人可以幫我解決這個問題嗎?非常感謝!
Please, could someone help me with this issue? Many thanks!
推薦答案
你可以嘗試刪除raw和github之間的點:
You could try and delete the dot between raw and github:
https://rawgithub.com/raysan5/raylib/master/examples/ex01_basic_window.c
參見 rawgithub.com,在這個 博文:
GitHub 不鼓勵這樣做,因為他們希望 repo 所有者使用 Github Pages 來托管其文件的特定版本.他們通過使用 Content-Type: text/plain
而不是 Content-type:application/javascript
提供來自raw
"域的文件來阻止它.
GitHub discourages this, since they want repo owners to use Github Pages to host specific versions of their files. They discourage it by serving files from the "
raw
" domain withContent-Type: text/plain
instead ofContent-type:application/javascript
.
直到最近,當 Google Chrome 實施了一個安全修復程序,如果 JavaScript 的 Content-type
不正確,這將阻止 JavaScript 被執行,這才成為問題.
當您查看您無法控制的頁面時,這是有道理的.但是,當您決定要包含哪些腳本時,就很麻煩了.
This wasn’t a problem until recently, when Google Chrome implemented a security fix that prevents JavaScript from being executed if it has an incorrect Content-type
.
This makes sense when you’re viewing pages outside of your control. But when it’s you who’s deciding what scripts to include, it’s a hassle.
作為 Rob W 以下評論:
值得一提的是,此網絡服務解決 OP 問題的唯一原因是它包含 Access-Control-Allow-Origin: *
響應標頭.
It's worth mentioning that the only reason that this web service solves the OP's problem is that it includes the
Access-Control-Allow-Origin: *
response header.
這篇關于從 GitHub 讀取代碼作為網頁中的文本(原始)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!