0

How do I read contents from a server side file using javascript?

asked Sep 10, 2009 at 12:28
1
  • Do you mean from a web browser? Like an AJAX request? Commented Sep 10, 2009 at 12:30

5 Answers 5

10

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'
});
answered Sep 10, 2009 at 12:31
Sign up to request clarification or add additional context in comments.

1 Comment

Should be noted that the file must reside in the same domain as the request, otherwise a server side proxy will be necessary.
2

using Ajax (XmlHttpRequest) e.g. using jQuery:

jQuery.get( url, [data], [callback], [type] )

answered Sep 10, 2009 at 12:31

Comments

2

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.

answered Sep 10, 2009 at 12:31

Comments

2

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.

answered Sep 10, 2009 at 12:30

8 Comments

Sure it, is assuming the Javascript is running on the server, which it probably isn't but the question doesn't make that clear as yet
This can be done with plain javascript, no additional frameworks required: var xmlhttp; if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else if (window.ActiveXObject) { // code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } else { alert("Your browser does not support XMLHTTP!"); } xmlhttp.onreadystatechange=function() { if(xmlhttp.readyState==4) { document.myForm.time.value=xmlhttp.responseText; } } xmlhttp.open("GET","time.asp",true); xmlhttp.send(null);
I don't think a downvote was warranted, the answer is likely to be correct.
But you have to code from server side to process the AJAX request. Thats why I made the comment that it is not possible using plain javascript. Also I mentioned about AJAX in my answer.
@Nerdling: that would the AJAX then that phoenix was refering to right? It would have been better for you to place this code in an answer than to place that unformatted mess in the comments.
|
1

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.

answered Sep 10, 2009 at 12:31

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.