从 GitHub 读取代码作为网页中的文本(原始)

2023-05-14前端开发问题
11

本文介绍了从 GitHub 读取代码作为网页中的文本(原始)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在尝试从我的 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 with Content-Type: text/plain instead of Content-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 读取代码作为网页中的文本(原始)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

layui中表单会自动刷新的问题
layui中表单会自动刷新的问题,因为用到layui的表单,遇到了刷新的问题所以记录一下: script layui.use(['jquery','form','layer'], function(){ var $ = layui.jquery, layer=layui.layer, form = layui.form; form.on('submit(tijiao)', function(data){ a...
2024-10-23 前端开发问题
262

JavaScript小数运算出现多位的解决办法
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18 前端开发问题
301

jQuery怎么动态向页面添加代码?
append() 方法在被选元素的结尾(仍然在内部)插入指定内容。 语法: $(selector).append( content ) var creatPrintList = function(data){ var innerHtml = ""; for(var i =0;i data.length;i++){ innerHtml +="li class='contentLi'"; innerHtml +="a href...
2024-10-18 前端开发问题
125

JavaScript(js)文件字符串中丢失"\"斜线的解决方法
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转...
2024-10-17 前端开发问题
437

layui中table列表 增加属性 edit="date",不生效怎么办?
如果你想在 layui 的 table 列表中增加 edit=date 属性但不生效,可能是以下问题导致的: 1. 缺少日期组件的初始化 如果想在表格中使用日期组件,需要在页面中引入 layui 的日期组件,并初始化: script type="text/javascript" src="/layui/layui.js"/scrip...
2024-06-11 前端开发问题
455