Jump to content
Sign in to follow this  
Rewillis

AJAX script.

Recommended Posts

Attention: W3schools Javascript forum members:

     Earlier this morning I coded a simple AJAX script to post a line of text and a button to a web page and then, upon clicking the button,  to print the text ( in the "ajax_1.txt" file ) to the web page.  The AJAX script is as follows:

<!DOCTYPE html>
<html>
<body>

<div id="demo">
  <h1>Let AJAX print this text</h1>
  <button type="button" onclick="loadDoc()">Print Content</button>
</div>

<script>
function loadDoc() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
    document.getElementById("demo").innerHTML = this.responseText;
    }
  };
  xhttp.open("GET", "ajax_1.txt", true);
  xhttp.send();
}  
</script>

</body>
</html>  

The file "ajax_1.txt" is as follows:

Name = Russell E. Willis.
Birth Date = 12/21/1966.
Age = 52.

The line of text and the "Print Content" button appear in the web browser ( as indicated in the "<div-</div>" portion of the script file above ( but I was unable to insert the screen capture file of the output into this posting due to space limitations )).  Why is the text file not appearing in my web browser when I click on the "Print Content" button?  What am I doing wrong?  NOTE: The script file above is just a minor variation of an example provided on your web page.  Thank you ( see Colossians 3:15, for example ) for your help.  Keep in touch.🙂

Sincerely in Christ,

Russell E. Willis

P.S. - Please read Proverbs 23:23.

 

 

Edited by Rewillis

Share this post


Link to post
Share on other sites

If this is running in your local filesystem then your browser security settings may be preventing scripts from accessing the text file. It is best to test AJAX in a real server environment.

Share this post


Link to post
Share on other sites

Attention: Ingolme:

     Thank you ( see Colossians 3:15, for example ) for your suggestion.  I set up a new test subdomain on the web hosting site I am using for two of my web pages, uploaded the AJAX ( Asynchronous JavaScript And XML ) file, and the "ajax_1.txt" file,  and the AJAX file works perfectly.  Keep in touch.🙂

Sincerely in Christ,

Russell E. Willis

P.S. - Please read Proverbs 23:23.

Edited by Rewillis

Share this post


Link to post
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
Sign in to follow this  

×