0

Im using a javscript file to read JSON data and send them to my html file, everything is in a file, nothing is on a server, but it wont work, error syntax:

Access to XMLHttpRequest at 'file:///C:/Users/USER/Desktop/Websites/PROMO%20CODES/jpr.JSON' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, chrome-untrusted, https.

index.js:10 GET file:///C:/Users/USER/Desktop/Websites/PROMO%20CODES/jpr.JSON net::ERR_FAILED

MY CODE

function getData() {
    var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
  if (this.readyState == 4 && this.status == 200) {
    var myObj = JSON.parse(this.responseText);
    document.getElementById("demo").innerHTML = myObj.name;
  }
};
xmlhttp.open("GET", "jpr.JSON", true);
xmlhttp.send();
}
  • 1
    in chrome /edge chrome at least - `file:///` is considered cross origin even if file is loaded using `file:///` protocol --- use a http server - see how `file` is not in the list `http, data, chrome, chrome-extension, chrome-untrusted, https` – Jaromanda X Sep 20 '20 at 23:25
  • I dont get it, what should I do then – Yazeed zaid Sep 20 '20 at 23:26
  • use a http server - you've tagged the question with `php` ... so you'll need some sort of http server for that at least – Jaromanda X Sep 20 '20 at 23:26
  • A quick workaround is to use a script like `const data = { ... };` instead. As for the server, use Apache, or XAMPP, or node. –  Sep 20 '20 at 23:27
  • Put the promo-codes on your server. If you want, secure them in a `restricted` folder with permission 700, then use another file outside the `restricted` folder that you `` on, under post conditions not shown here. – StackSlave Sep 21 '20 at 00:00

0 Answers0