I am writing a web-based application where I need to access the txt file that is hosted online and read the file. I am using following code to do it:
var oReq = new XMLHttpRequest();
oReq.addEventListener("load", function(){
// parse the file
}
})
oReq.open("GET","https://bahadorsaket.com/others/ranking.txt");
oReq.send();
I got this error:
Failed to load https://bahadorsaket.com/others/ranking.txt: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
After searching and reading this post, I tried changing my code to this.
var oReq = new XMLHttpRequest();
oReq.addEventListener("load", function(){
// parse the file
}
})
oReq.setRequestHeader( 'Access-Control-Allow-Origin', '*');
oReq.open("GET","https://bahadorsaket.com/others/ranking.txt");
oReq.send();
This time I got this error:
Failed to execute 'setRequestHeader' on 'XMLHttpRequest': The object's state must be OPENED.
Any idea how to fix this problem? I am very new to these kinds of stuff!!!