Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems fetching a md file using javascript fetch()

I am currently developing a web app, where most of the content is written in markdown. So for handling this, I thought I could create a github repo to host all of the markdown files and then use the fetch() api to grab the files from github.

My code looks like so:

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

I am getting this error though when I do that:

Failed to load https://github.com/erasabi/trekthroughs/blob/master/pen_testing/RickdiculouslyEasy.md: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

Is there anyway to go about doing this? End result is once I fetch the markdown file's content I would like to use showdown or markedjs to convert the content into html for the site.

like image 358
Scott Avatar asked Sep 17 '25 09:09

Scott


1 Answers

Figured it out basically you got to do something like this:

fetch('https://raw.githubusercontent.com/erasabi/trekthroughs/master/pen_testing/RickdiculouslyEasy.md') 
    .then(response => response.text())
    .then(result => document.getElementById('content').innerHTML = marked(result));
like image 159
Scott Avatar answered Sep 19 '25 00:09

Scott