Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript: txtFile.open

Status
Not open for further replies.

ElliottClarke

Programmer
Mar 8, 2011
2
GB
Hi Everyone,

Looking for some help. I'm a PHP developer quite new to Javascript. I'm currently trying to parse some information out of a text file and have started by using txtFile.open . My code is:

var txtFile = new XMLHttpRequest();
txtFile.open("GET", "../beers.txt", true);
txtFile.onreadystatechange = function() {
if (txtFile.readyState === 4) {
if (txtFile.status === 200) {
allText = txtFile.responseText;

lines = txtFile.responseText.split("\n");
var numberoflines = count(lines);

However, I keep getting 'Access is Denied' error for the txtFile.open("GET"... line.
I've tried every other method - file:/// etc
It DOES work using a remote HTTP address ( on my localhost but the whole point of this is that I want it to be accessible without using a server / the internet. Any help is gratefully received.

Elliott.
 
Hi

[tt]XMLHttpRequest[/tt] is subject of Same origin policy restriction.

That restriction can be overridden using Cross-Origin Resource Sharing, but
[ol]
[li]that must be allowed on server-side ( see enable cross-origin resource sharing )[/li]
[li]old clients does not support it ( see User Agents Implementing CORS )[/li]
[/ol]
( If you are playing with something for yourself only, see the Greasemonkey FireFox extension. Its [tt]GM_xmlhttpRequest()[/tt] function has no same origin limitation. However it is not allowed to use file:// protocol. )

If I understood your problem correctly. If not, please provide more details.


Feherke.
 
Hi Feherke,

Thanks for that. I'll certainly look into it and give it a go.

The problem is - that the file where the above code is, is in the SAME directory as the text file - so they are both the same origin aren't they?

All I want to do really (without any need of server side language if possible) is to read (using Javascript) a text file and parse it using delimiters.

Thanks
Elliott
 
Hi

Elliott said:
The problem is - that the file where the above code is, is in the SAME directory as the text file
Then probably no restriction applies. Supposing you are using the same protocol too. ( I mean, http:// and https:// is not the same origin. )

Could you post an URL to a publicly accessible version of that page ? ( As simple as it is secure for your business, but manifesting the described error. )


Feherke.
 
Soluzione trovata da modesto programmatore siciliano ;) :

"c:\\directory\\sub_directory\\sub_sub_directory\\test.txt"


funziona perfettamente in iexplorer 8, also not in firefox :( !!

Regards Renatko
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top