Jump to content

How to print the plain text file retrieved on webpage


roseli98

Recommended Posts

Dear all,Again, this is my first UI project. I am trying to retrieve data from a plaintext file "hello.txt' and print it on the screen. Alert works fine but I do need to save the data into an array or string(?), then print them out.However, I've been frustrating since print, show don't seem to work. I will parse the file and print it out using the correct format. But is there anyway that I can print them out to make sure they are here?My code looks like:<!DOCTYPE html><head><title>Ajax Test</title><script type="text/javascript"> var xmlHttp = new XMLHttpRequest(); xmlHttp.open("GET", "hello.txt", true); xmlHttp.addEventListener("load", ajaxCallback, false); xmlHttp.send(null); function ajaxCallback(event){ alert( "Your file contains the text: n" + event.target.responseText ); }</script></head><body></body></html>Hello.txt is: 08/28/2013 16:58:20 68 08/28/2013 16:58:21 299 08/28/2013 16:58:22 403 08/28/2013 16:58:23 1008 08/28/2013 16:58:24 1139 08/28/2013 16:58:25 926 08/28/2013 16:58:26 1287 08/28/2013 16:58:27 1457 08/28/2013 16:58:28 1175 08/28/2013 16:58:29 1207 08/28/2013 16:58:30 1132 08/28/2013 16:58:31 1196 08/28/2013 16:58:32 1362 08/28/2013 16:58:33 1255 08/28/2013 16:58:34 1243 08/28/2013 16:58:35 1173 08/28/2013 16:58:36 1187 08/28/2013 16:58:37 1354 08/28/2013 16:58:50 1292 08/28/2013 16:58:51 1205 08/28/2013 16:58:52 1258 08/28/2013 16:58:53 1261

Link to comment
Share on other sites

Where is the code to actually put it on the page? i don't see that anywhere in your code. I would expect you to be setting the responseText to the innerHTML property of some element on your page at minimum.

Link to comment
Share on other sites

Hi, Dave,It's not working. I put both files on the same path.I tried the sample file you sent, copied and pasted the html as .html, and 2 lines of Ajax as ajax_info.txt. Nothing was sent back after I clicked the button. Did I miss anything?

Edited by roseli98
Link to comment
Share on other sites

Hi, thescientist, There are 2 files here. One is .html, the first 1/2. One is hello.txt. Both files will be running on internal server so no security issues. What I am trying to achieve is to retrieve data from hello.txt and expect to show it on the webpage.I put them on the same location. If you try that, you will see the pop up window called by Alert.I am new to Ajax, XML and Javascript and wonder whether there is any to by pass using all of them to minimize the complexity.Thanks.

Edited by roseli98
Link to comment
Share on other sites

Follow the example Dave J provided and show us the code you are using that actually takes the responseText and displays it on the page (since you say the alert part works already, so you are getting the response correctly). For now, just getting it to work like the tryit example would be a good first step

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...