This used to work perfectly, now mysteriously it doesn't...
I have a first html page with a link. By doing click
in the link I want to bring content from another page through Ajax
and display it in a div
on the first page.
The error occurs in the line that loads the "ajax-content-html" file:
$("#destino").load("contenido-ajax.html");
The error that the Chrome browser gives is the following:
Access to XMLHttpRequest at 'file:///C:/misphp/contenido-ajax.html' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome -extension, https.
If I open the Mozilla browser it works fine. Before it also worked fine with Chrome and now it doesn't. I have tried on two more computers with Chrome and they give the same error...
This error occurs locally, if I upload it to a server it runs correctly.
Why doesn't it work locally?
Thank you
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js" type="text/javascript"></script>
<script>
$(document).ready(function()
{
$("#enlaceajax").click(function(evento)
{
evento.preventDefault();
$("#destino").load("contenido-ajax.html");
//alert("");
});
})
</script>
</head>
<body>
<a href="#" id="enlaceajax">Haz clic!</a>
<br>
<div id="destino"></div>
</body>
</html>
What happens is that it needs a web server to run correctly, if you use Linux you can install Apache and put your project in the folder
/var/www/html
, and in the browser run it as:localhost/capeta_proyecto
You can also try it with Firefox and it will work; For security, in the new Google updates, it is no longer allowed to run applications that use Ajax, JavaScript or JQuery without a web server (which use protocol
http
)A quick and effective solution can be to use the Chrome extension
Web server for chrome
, which you can download from hereOnce installed, you just have to choose the directory where you have your web page and it will upload it to that server:
My solution was to run the entire site on a local server using http-server , like this: