Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3969342
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T03:57:08+00:00 2026-05-20T03:57:08+00:00

server: apache http server 2.2 problem: code does not load my text file hello

  • 0

server: apache http server 2.2
problem: code does not load my text file

hello people. im currently trying to learn javascript and am following several tutorials. written below is a code off of w3schools. what its supposed to do is change the displayed text upon clicking the button. however, this does not work for me. the html file where the code below is save and the text file im trying to open are in the same folder.
im accessing the html file off of chrome using this: http://localhost/ajaxtutorial.html. while it does display the html file correctly, upon clicking the button nothing happens. ive tried changing the file to php and making an equivalent php file to the said text file but still nothing happens. please help.

<html>

<script type="text/javascript">

//comments are from http://www.tizag.com/ajaxTutorial/ajaxxmlhttprequest.php

    function loadXMLDoc(url , cfunc)
    {
        var xmlhttp;

        //XMLHttpRequest object is used to exchange data with a server behind the scenes
        //creates an xmlhttprequest object
        if (window.XMLHttpRequest)
        {// code for IE7+, Firefox, Chrome, Opera, Safari
          xmlhttp=new XMLHttpRequest();
        }
        else
        {// code for IE6, IE5
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }

        //The XMLHttpRequest object has a special property called onreadystatechange
        //onreadystatechange stores the function that will process the response from the server
        //every time the "ready state" changes, this function will be executed
        xmlhttp.onreadystatechange=cfunc;

        xmlhttp.open("GET",url,true);
        xmlhttp.send(null);

    }   

    function myFunction()
    {
        loadXMLDoc
        ("ajax_info.txt",           
          function()
          {
            //The XMLHttpRequest object has another property called readyState
            //This is where the status of our SERVER'S RESPONSE is stored
            //The SERVER RESPONSE can be processing, downloading or completed
            //When the property readyState is 4 that means the response is complete and we can get our data

            //Download worked as intended, data request successful if status property = 200

            if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
            }

          }               
        );
    }

</script>

<div id="myDiv">
    <h2>Let AJAX change this text</h2>
</div>

<button type="button" onclick="myFunction()">Change Content</button>

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T03:57:09+00:00Added an answer on May 20, 2026 at 3:57 am

    In myFunction the xmlhttp variable is not in the scope of the function. This should be causing a JavaScript error, which you can view in Chrome by going to Menu > Tools > JavaScript console. One way to fix this would be to pass the xmlhttp object as a parameter.

    function loadXMLDoc(url , cfunc) {
        //some code...
        xmlhttp.onreadystatechange=function() {
            //pass xmlhttp as a parameter to this function and preserve the context
            //see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/call
            cfunc.call(this, xmlhttp);
        }
        //some more code...
    }
    
    function myFunction() {
        loadXMLDoc("ajax_info.txt", function(xmlhttp) {
            //xmlhttp is now in scope because we passed it as a parameter
        });
    }
    

    Update

    I created a working example at http://jsfiddle.net/6rPgE/

    As for your question about the modifications I suggested…
    The second parameter to the loadXMLDoc method (cfunc) is a function. In this case, an anonymous function is created inside myFunction which will be passed as the cfunc parameter to loadXMLDoc. When the onreadystatechange callback is invoked, the cfunc function is called with xmlhttp as the first parameter. This parameter is passed into the anonymous function defined inside myFunction, and is responsible for actually doing something with the AJAX response. On an entirely different note, I highly recommend using a debugger (Chrome has one built-in) and the information provided by the browser’s error console to assist you in debugging these issues in the future. Learning how to use a debugger will save you countless hours of banging your head against the wall.


    Update 2

    Just thought it would be nice to look at how this can be done using jQuery with quite a bit less code. AJAX is one area where it can be really nice to use a library that abstracts away the details.

    Another example that uses jQuery at http://jsfiddle.net/j9QvE/1/


    Update 3

    Note that in my code I replaced the path to ajax_info.txt with a path specifically used for testing AJAX functionality in jsFiddle (/echo/js/?js=Success!). This was necessary because ajax_info.txt does not exist on the jsFiddle servers, so requesting it would have resulted in a 404 error. Don’t forget to change the path to point to an appropriate resource on your own domain.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My Apache server works fine without -DSSL, but does not start with SSL. This
Im trying to learn php by doing a little project using apache server. I
I have my Apache http server running on localhost:80 and restlet server on localhost:8182,
We have Tomcat application server set up at port 8080 and Apache Http Server
I'm trying to add Apache server to Eclipse(Juno). I have installed Apache server 7.0,
I am trying to make a Http POST request using apache HTTP client. I
Trying to get ActiveMQ with JNDI running following ( http://activemq.apache.org/jndi-support.html ) and i have
I created a simple Apache server module following a sample hello world module I
I'm trying to send a file to the user using xsendfile within the code
I've got a weird problem here: I'm getting a 500 error code from Apache

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.