我想使用 fetch API 加载 .md 文件,并且需要使用 marked
解析它
我在通过 fetch 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)来解析它。
如有任何帮助,我们将不胜感激。
最佳答案
我假设您收到请求的资源上不存在“Access-Control-Allow-Origin” header 。
错误响应。也就是说,您正在发出跨域请求,从而违反了 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));
这应该使请求成功。不过,这只是为了测试。
关于javascript - 使用 fetch api 远程加载 md 文件并在客户端/浏览器上解析它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43599592/