There is a javascript file, the following code is in js / script.js in it:

var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (xhttp.readyState == 4 && xhttp.status == 200) { genProducts(xhttp); } }; xhttp.open("GET", 'products.xml', true); xhttp.send(); 

JS code is inserted on a couple of pages in different folders, when you try to download them, throws an error:

 S_ERROR_DOM_BAD_URI: Access to restricted URI denied 

When you load index.html (i.e. where products.xml are next to it) everything is fine. How to write the normal way or there are other options to read the XML with the DOM model. thank

  • How does index.html run? - Grundy
  • opens with browser - user_21
  • how? just like a file from disk? Which URL is visible in the address bar of the browser? - Grundy
  • Well raised Denver, and went to the site. Tries to look for xml in folders where html is located which I open - user_21
  • and where is the file really? - Grundy

2 answers 2

In the absence of a leading slash in the address, it is regarded as a path relative to the current address.

If a leading slash is present, the address is calculated relative to the site root.

Thus, as a solution, you can use two options:

  1. relative to the current page (assuming that "folder1" is in the root of the site, and "products.xml" is also in the root of the site)

     "../products.xml" 
  2. relative to the site root (assuming that "products.xml" is at the root of the site):

     "/products.xml" 
  • Thanks, on the hosting it helped, with this option when opening a file without hosting, then script.js: 9 XMLHttpRequest cannot load file: /// D: /html/products.xml. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource. can't - user_21
  • @ user_21, that is, in general, xmlhttprequest requests should not work without a server. In the extreme case, you can try to write the full path to the file, including the file:/// protocol, but most likely it will not work either - Grundy
  • Thanks, and there are other options to read xml? - user_21
  • @ user_21, in a sense? you can try to use xslt, and run not index.html, but products.xml right away - Grundy
  • yes it is just necessary on xml :( - user_21

I can be wrong, but the open method configures your connection, maybe it makes sense to put it in front of the connection itself?

 var xhttp = new XMLHttpRequest(); xhttp.open("GET", 'products.xml', true); xhttp.onreadystatechange = function() { if (xhttp.readyState == 4 && xhttp.status == 200) { genProducts(xhttp); } }; xhttp.send(); 

  • Nope, onreadystatechange is an event that happens only after xhttp.send() , so let it set where it wants is not critical. - user207618