roseli98 Posted January 17, 2014 Share Posted January 17, 2014 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 More sharing options...
davej Posted January 17, 2014 Share Posted January 17, 2014 (edited) Have you looked at this example?http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_firstNow to be clear, the txt file in this example is located on the server. Edited January 17, 2014 by davej Link to comment Share on other sites More sharing options...
thescientist Posted January 17, 2014 Share Posted January 17, 2014 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 More sharing options...
roseli98 Posted January 18, 2014 Author Share Posted January 18, 2014 (edited) 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 January 18, 2014 by roseli98 Link to comment Share on other sites More sharing options...
roseli98 Posted January 18, 2014 Author Share Posted January 18, 2014 (edited) 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 January 18, 2014 by roseli98 Link to comment Share on other sites More sharing options...
thescientist Posted January 18, 2014 Share Posted January 18, 2014 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 More sharing options...
roseli98 Posted January 18, 2014 Author Share Posted January 18, 2014 Dave & thescientist, Thanks for pointing out the right track to me. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now