0

I am trying to import external HTML-code into a document with jQuery but it doesn't seem to work. There is no occurence of the paragraph "I'm some text" and i need help figuring out why.

I am using the .load function as following:

$('#yourDiv').load('header.html #readMe');

The reference to the imported element looks like this:

<div id="yourDiv"></div>

The external HTML-file (header.html) looks like this:

<div><div id="readMe"><p>I'm some text</p></div></div>

The document with the query is located in the same directory as "header.html". I've tried changing the path with "/header.html" & "./header.html" but with no success.

This is what I see when I open up by browser console:

"Access to XMLHttpRequest at 'file:///C:/Users/correctpath' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, chrome-untrusted, https, isolated-app."

Kostas Minaidis
  • 4,681
  • 3
  • 17
  • 25
ottdevott
  • 25
  • 4
  • 1
    Any errors in your console? – j08691 Sep 30 '22 at 14:53
  • 2
    You need to debug the request. First open devtools in your browser, normally by pressing F12, then check the console for errors. Also check the network tab after the `load()` line has been invoked to see the state of the request. Anything other than a 200 OK response needs to be investigated on your server-side. – Rory McCrossan Sep 30 '22 at 14:56
  • Yes! "Access to XMLHttpRequest at 'file:///C:/Users/correctpath' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, chrome-untrusted, https, isolated-app." – ottdevott Sep 30 '22 at 14:57
  • You are probably testing this by directly opening up the file on the browser (e.g. double clicking the HTML file). You should never develop this way. Instead use a local web server (e.g. VSCode Live Server extension) to run the app via a local web server that resembles how HTML content is served on the Web. – Kostas Minaidis Sep 30 '22 at 15:01

0 Answers0