I describe my problem. I'm doing simple example with ajax. I have on the one hand the html with a button where when I click I should show a message "this is a message using ajax" (which is in a txt).
First I was given an error of type XMLHttpRequest can not load "file path". Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.
Which I have been able to find a solution:
When I try to execute an Ajax code inside an html in local with Chrome, the Ajax code does not run for security. The error that the console gives me in Chrome is: XMLHttpRequest can not load "file path". Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.
The best solution is to disable that security in order to run Ajax, for this I first close Chrome, because if there is an open window it does not work.
Then I access the console and write the Chrome path followed by the following parameters, in the case of windows it is:
"C: \ Program Files (x86) \ Google \ Chrome \ Application \ chrome.exe" --allow-file-access-from-files
By doing this I open a Chrome window and there we can enter the local url and Ajax will work without problems. Once I close all Chrome windows this option will be disabled again, so we should repeat these actions again so we can do it again.
So far everything is correct. Once this is done, the browser gives me the following error when executing the code: Failed to load resource: net :: ERR_FILE_NOT_FOUND
Which I do not know how to solve. I imagine it will be very simple but I do not know how to get to the solution.
My source code is:
<!DOCTYPE html> <!-- Definición HTML5 del documento -->
<html lang="es">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title></title>
</head>
<style type="text/css">
<
<body>
<button onclick="ejecutarAJAX()">Hazme Click</button>
<div id="info"></div>
<script type="text/javascript">
function ejecutarAJAX()
{
var ajaxRequest;
if(window.XMLHttpRequest){ //navegadores tipo chrome
ajaxRequest = new XMLHttpRequest();
}else{ //navegadores antiguos (ie)
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
//0 Petición no ha sido inicializada
//1 Petición no ha sido establecida
//2 Petición no ha sido enviada
//3 Petición esta siendo procesada
//4 Petición ha sido finalizada
var ajaxRequest = new XMLHttpRequest();
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4 && ajaxRequest.status == 200){
document.getElementById("info").innerHTML = ajaxRequest.responseText;
}
}
ajaxRequest.open("GET", "documento.txt", true);
ajaxRequest.send();
}
</script>
</body>
</html>