Ajax Issues adding text to site, from txt file

Discussion in 'jQuery' started by samie, Mar 28, 2013.

  1. #1
    Hi Guys,

    Sorry if this is the wrong category. I'm just wondering if I can get some assistance with trying to add text to my site from a txt file.

    I've already added in the following code into my header,

    <script>
        var content
     
        function loadXMLDoc()
        { 
            var textfile;
            if (window.XMLHttpRequest)
            {
                textfile = new XMLHttpRequest();
            }
            textfile.onreadystatechange = function ()
            { 
                if (textfile.readyState == 4 && textfile.status == 200)
                {
                    content = textfile.responseText;
                }
            }
            textfile.open("GET", "localhost/files/myfile.txt", true);
            textfile.send();
        }
     
    </script>
    Code (markup):
    Then in my HTML source I'm inserting the following,
    <script type="text/javascript">
      loadXMLDoc();
    </script>
    Code (markup):
    and it's not working. I've tried various different variations of trying to call this function and I'm really new to the ajax side of things.

    Any suggestions?
     
    samie, Mar 28, 2013 IP
  2. kutchbhi

    kutchbhi Active Member

    Messages:
    130
    Likes Received:
    4
    Best Answers:
    2
    Trophy Points:
    70
    #2
    Probably a stupid question, but are you running a server on localhost to send the txt file over ? Something like xampp, wamp
     
    kutchbhi, Mar 29, 2013 IP
  3. samie

    samie Member

    Messages:
    269
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    45
    #3
    Well currently it's on a web hosting server :p But I do also have Xampp installed and have been trying it on XAMPP. I only threw in Localhost there just so I wasn't publicly sharing the URL. But now that I think about it "localhost/ is going to be looking for a subdirectory instead of "http//localhost/", but either way I've tried it with my domain as well.

    Can't seem to find anything online for accomplishing this, almost thinking maybe ajax was not meant for loading text from a file onto a page, but more for onclick events. I can accomplish this easily using PHP, but I guess for learning purposes and so I didn't have to use PHP.
     
    samie, Mar 29, 2013 IP
  4. kutchbhi

    kutchbhi Active Member

    Messages:
    130
    Likes Received:
    4
    Best Answers:
    2
    Trophy Points:
    70
    #4
    well what does the network tab in firebug says. That's how I usually diagnose ajax problem.
     
    kutchbhi, Mar 29, 2013 IP