1

I trying to read txt file in JS. So I am using this code.

My txt file looks: ["187.48.35.40"]

My application is searching IP adress on Google Map.

When i put in my code: var ip = ["187.48.35.40"]; My program works perfect but I want to open txt file with IP adres.

function readTextFile(file)
{
var rawFile = new XMLHttpRequest();
rawFile.open("GET", file, false);
rawFile.onreadystatechange = function ()
{
    if(rawFile.readyState === 4)
    {
        if(rawFile.status === 200 || rawFile.status == 0)
        {
            var allText = rawFile.responseText;
            alert(allText);
        }
    }
}
rawFile.send(null);
}
var ip = readTextFile("xxx.txt");

But my application can't read txt file.

Kenny Pattern
  • 49
  • 1
  • 9
  • 1
    Are you getting any errors? – j08691 Jan 30 '18 at 19:58
  • There is no any errors. – Kenny Pattern Jan 30 '18 at 20:02
  • According to the code you've linked, you have to provide the full file url, including the `file://` protocol part. Keep in mind that this will only work for local files. If you're expecting the file to be on the server, you have to set up the server accordingly and use the url to the file on the server. – Marko Gresak Jan 30 '18 at 20:05
  • @MarkoGrešak I tried that but not working. – Kenny Pattern Jan 30 '18 at 20:07
  • @KennyPattern what exactly did you try? Can you add this info to the question? Also, if the server is public, please provide a link to it, it would be easier to debug this way. – Marko Gresak Jan 30 '18 at 20:09
  • Open your developer's tools and look at the Network tab while you make the request. It will show you the response code and the response itself. – Scott Marcus Jan 30 '18 at 20:09
  • There is an error like: index.html:40 Failed to load file:///Users/xxx/Desktop/googleMaps/src/googlemaps/xxx.txt: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https. – Kenny Pattern Jan 30 '18 at 20:10
  • 1
    Chrome will consider this a "Cross origin request", and refuse it. Firefox will allow it, provided that the file is in the same folder as your HTML file that runs the script. The definition of what is cross origin is not clearly defined for the `file:///` protocol. Move your script to a server (can be localhost). – trincot Jan 30 '18 at 20:10
  • 1
    `XMLHttpRequest` doesn't work unless you are requesting from a server over HTTP or HTTPS. Even if you want this to run locally, you have to be running a local web server and making your request against that. As such, the location of the resource must also be prefixed with the protocol (i.e. `http://` of `https://`). – Scott Marcus Jan 30 '18 at 20:11

1 Answers1

1

XMLHttpRequest doesn't work unless you are requesting from a server over HTTP or HTTPS. Even if you want this to run locally, you have to be running a local web server and making your request against that. As such, the location of the resource must also be prefixed with the protocol (i.e. http:// of https://).

Scott Marcus
  • 64,069
  • 6
  • 49
  • 71