Error with the method open from XMLHttpRequest

Hello all,
First of all, I would like to inform you that I’m not an expert in Javascript, I am much more C low level oriented, but I need to realize this job. Many people will tell me to go to learn Javascript on basic tutorials, this can be true but I already know pretty much code environment in general, and I fear to spend too much time learning Javascript without finding exactly what I need. I also know a little bit of COO (Qt) and I understand pretty much Javascript behavior (I guess…) so I take the opportunity to explain my problem here:
I try to parse an XML file in order to obtain a HTML page which display in the end a dynamic array with which I could, depending on the checked boxes, check the corresponding boxes, like on the following picture:

Lines are elements of my XML file, and columns are children of my elements. This is let’s say my final goal. To be honest, I actually struggle to load my XML file from Javascript. I have been so on Google to read some topics:

Sources:
XML Parser (French classes)
https://openclassrooms.com/courses?q=parser+xml

load XML without open method (French forum):
https://openclassrooms.com/forum/sujet/lire-fichier-xml-en-javascript-72124

XML text parser (thing that I don t want, I want to parse a file):
https://www.w3schools.com/xml/xml_parser.asp

Answer from a forum to parser a local XML:
https://stackoverflow.com/questions/30920325/how-to-read-local-xml-file-using-xmlhttp

API to load files in Javascript:
https://www.html5rocks.com/en/tutorials/file/dndfiles/

In a first time, here is my code:

//function loadXMLDoc() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById(“demo”).innerHTML = xhttp.responseText;
}
};
console.log(‘salut les potes’);
xhttp.open(“GET”, “FileArray.xml”, true);
xhttp.send();
//}

And the error I get using Chrome:
XMLHttpRequest cannot load file://C:Users/…
Cross origin requests are only supported for protocol schemes:http, data, chrome, chrome-extension, https
About IE error, I directly got “Access Denied”.
I looked in Google, and apparently this error comes because of a protection which does not allow Javascript to do files requests on URLs, protection which sounds normal regarding security point of view:
Explanation for why open method redirects to an error:
https://stackoverflow.com/questions/20041656/xmlhttprequest-cannot-load-file-cross-origin-requests-are-only-supported-for-ht
error after open call (forum):
https://stackoverflow.com/questions/10752055/cross-origin-requests-are-only-supported-for-http-error-when-loading-a-local
My global question is the following so:
Can I pass over this problem, and so this error? I read somewhere that launching chrome with a specific extension (I don t exactly remember what, but I tried unsuccessfully), try it with firefox I won t have this problem, or install a local server on my computer, thing that I’d like to avoid because I would need more skills for this, and my script is destined to be portable, so the problem would appear on a new computer if launched from another one.
To be simple, what could be the easiest method, even the easiest library to take to load this file locally? I quickly read that JQuery is a very good one for this…Did not try yet. I don’t know yet which way to take for starting this job, and I fear to loose time trying the wrong one. My goal is not to become a Javascript expert, but just to load my XML file not too hardly.
Thanks for everyone who will read this post, hoping I ll have few answers that will open ways to me.
Thank you everybody !

Greg

Hi @bakl0u! Well your request will eventually go to a server, so it makes sense to develop in an appropriate environment. The easiest solution would be to just upload your page and data to an actual server-- you can get some space for free here and there. Of course, this would be rather tedious though, as you’d have to constantly upload your files.

Another very simple solution would be to use the built-in PHP development server – you just need to install PHP if you haven’t already, and then you can run

php -S localhost:8000 # or whatever port

in your working directory. There’s no actual PHP knowledge required.

If you need a full blown server with database and all, you might look into xampp or vagrant… but if all you want to do is serve a single XML file, the PHP dev server should fully suffice. :-)

Hi ! Sorry for the last answer, but in the end I found a solution pretty easy to deploy ! I add the following extension on Chrome : " --allow-file-access-from-files" and after restarting it, it allows me to load local file :smile:
Pretty good solution for what I needed !
Thank you however for the tips !

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.