How to upload content from other files using javascript?

Asked

Viewed 58 times

-2

I have a field:

<div class="conteudo"></div>

.

I’m wanting to put the text sequinte inside the content div

<p>Conteúdo</p>

.

To try to achieve this goal I am using the following script:

var output=document.querySelector("div.conteudo");
var input=new XMLHttpRequest();
input.open("GET","conteudo.html")
input.addEventListener("load",function(){
  output.innerHTML=input.responseText;
})
input.send();

.

But I’m getting the following error:

Access to Xmlhttprequest at 'file://D:/test/conteudo.html' from origin 'null' has been blocked by CORS policy: Origin cross requests are only supported for Protocol schemes: http, data, Chrome, Chrome-Extension, Chrome-untrusted, https.

.

How do I load html objects dynamically using javascript?

1 answer

1


For local testing it is not possible to call a file because the browser blocks because it does not identify the origin of the call, CORS, mainly in Chrome.

The suggestion is to use a micro server, type npm serves or python -S to call these files by URL inside your JS.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.