Reputation: 7428
How do I read contents from a server side file using javascript?
Upvotes: 0
Views: 1106
Reputation: 187040
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.
Upvotes: 2
Reputation:
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.
Upvotes: 2
Reputation: 14938
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.
Upvotes: 1
Reputation: 42343
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'
});
Upvotes: 10
Reputation: 9020
using Ajax (XmlHttpRequest) e.g. using jQuery:
jQuery.get( url, [data], [callback], [type] )
Upvotes: 2