使用 fetch api 远程加载 md 文件并在 client/browser 上解析它

Load md file remotely using fetch api and parse it on client/browser

我想使用 fetch API 加载一个 .md 文件,我需要使用 marked

来解析它

我在通过获取 api 抓取文件时遇到问题,我在 response.blob()response.arrayBuffer().

中什么也得不到
fetch('http://s3.amazon.com/some_bucket/some_file.md')
 .then(response => response.blob())
 .then(result => console.log(result));

然后我想获取结果并将其传递给 React 组件以进行渲染。我将使用标记(来自 npm)解析它。

如有任何帮助,我们将不胜感激。

我假设您收到了 No 'Access-Control-Allow-Origin' header is present on the requested resource. 错误响应。也就是说,您正在发出跨域请求,因此违反了 Same-origin policy.

基本上,您需要在 S3 存储桶上启用跨源资源共享 (CORS)。具体怎么做,可以read here.

此外,作为一个快速测试,您可以在您尝试获取的 URL 前加上 https://crossorigin.me/ 前缀,如下所示:

fetch('https://crossorigin.me/http://s3.amazon.com/some_bucket/some_file.md')
 .then(response => response.blob())
 .then(result => console.log(result));

这应该会使请求成功。不过,这只是为了测试。