How do I read contents from a server side file using javascript?
-
Do you mean from a web browser? Like an AJAX request?Adam Batkin– Adam Batkin2009年09月10日 12:30:16 +00:00Commented Sep 10, 2009 at 12:30
5 Answers 5
Ask the web server for it with Ajax. In jQuery speak, for instance:
jQuery.get('path/to/file/on/server.txt', null, function(data, status) {
// your file contents are in 'data'
});
1 Comment
using Ajax (XmlHttpRequest) e.g. using jQuery:
Comments
You have to serve the file via a HTTP request (i.e., the file is available as a URL like www.conphloso.com/somefile.txt), which you can grab via an ajax request in the background.
Comments
This is not possible using plain javascript. Javascript runs in the client browser and you cannot access a file in server. You can use AJAX to do this.
8 Comments
The quick answer is "you can't".
If you make the server side file accessible through your web server, you can use an xmlhttprequest, a.k.a ajax, to retrieve it.