gpt4 book ai didi

javascript - 使用 javascript fetch() 获取 md 文件时出现问题

转载 作者:行者123 更新时间:2023-11-30 14:39:25 24 4
gpt4 key购买 nike

我目前正在开发一个网络应用程序,其中大部分内容都是用 markdown 编写的。因此,为了处理这个问题,我想我可以创建一个 github 存储库来托管所有 markdown 文件,然后使用 fetch() api 从 github 获取文件。

我的代码是这样的:

fetch('https://github.com/erasabi/trekthroughs/blob/master/pen_testing/RickdiculouslyEasy.md')
.then(response => response.blob())
.then(result => console.log(result));

当我这样做时,我得到了这个错误:

无法加载 https://github.com/erasabi/trekthroughs/blob/master/pen_testing/RickdiculouslyEasy.md : 请求的资源上不存在“Access-Control-Allow-Origin” header 。因此不允许访问 Origin 'null'。如果不透明的响应满足您的需求,请将请求的模式设置为“no-cors”以获取禁用 CORS 的资源。

有什么办法可以做到这一点吗?最终结果是,一旦我获取了 markdown 文件的内容,我想使用 showdown 或 markedjs 将内容转换为网站的 html。

最佳答案

基本上你必须做这样的事情:

fetch('https://raw.githubusercontent.com/erasabi/trekthroughs/master/pen_testing/RickdiculouslyEasy.md') 
.then(response => response.text())
.then(result => document.getElementById('content').innerHTML = marked(result));

关于javascript - 使用 javascript fetch() 获取 md 文件时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49910833/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com