Aymen Kareem Aymen Kareem - 1 year ago 161
Javascript Question

Why XMLHttpResponse doesn't work

I have the following code which I can't figure out why it didn't work:

var request = new XMLHttpRequest();
request.open("GET", "textfile.txt", true);
request.onreadystatechange = function() {
if(request.readyState===4) {



I'm sure that I have the file name "textfile.txt" in the same directory as the html file. What I get is only an empty javascript alert but I can't display the text file text.

I noticed that I get the following error when I viewed the developer tools this error at the request.send(null) line:

XMLHttpRequest cannot load file:///D:/textfile.txt. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.

Answer Source

You need a web server, such as Nginx, Apache and IIS and then put your HTML file and textfile.txt in the same domain.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download