0

I want to get the string from a file in javascript but it doesnt' work. The error says that it is because Cross origin requests are only supported for HTTP. So the only reason this doesn't work is evidently because the HTML page isnt online. Is there a way to read a .txt file into a string that can workaround this issue

this is the code that doesn't work:

function readTextFile(file)

{
var File = new XMLHttpRequest();
File.open("GET", file, true);
File.send(null);
Text = File.responseText;
return Text;
}

At line 6 is stops and display the error.

Thanks in advance:)

  • Cross origin requests are manifesting only when you try to load files from different doman/subdomain. It's a common error that you request for example : http://www.some-domain.com/file.xml while the script is running on http://some-domain.com. Be sure that both, script and the file are on the same server and it should work out o f the box. – Goran.it Feb 03 '14 at 20:54
  • what is the value of file you are passing to this function. Is that a valid http url. See this [link](http://stackoverflow.com/questions/8449716/cross-origin-requests-are-only-supported-for-http-but-its-not-cross-domain) – pawinder gupta Feb 03 '14 at 20:58
  • What browser are you using? chrome, Firefox, IE#? – epascarello Feb 03 '14 at 20:58
  • i just gave up and have put my entire javascript application on weebly and it works on there, thanks for the help though. – Black 'n' Blue Mar 03 '14 at 22:18

1 Answers1

0

You cannot use it for a local file for security reasons I believe, it should work as long as you make the file variable a valid http url to the file. You can use xampp / wampp / mamp / etc to create a local webserver and run it from there !

that way your link can be like http://localhost/link/to/file.txt