2

I have a text file called sample.txt and I want to load the data of that file using following HTML file but it would not be loading.

$( document ).ready(function() {
  jQuery.get('sample.txt', function(theData) {
    alert(theData);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h1>Alert data from text file</h1>
<div><p id="fileData"></p></div>
Shiladitya
  • 12,003
  • 15
  • 25
  • 38
Joshua
  • 136
  • 14
  • Do you have errors in the console? – Andy Nov 20 '17 at 14:30
  • 2
    If you're running this under the local file system you'll be blocked by browser security. You'll need to run the AJAX request on a webserver. You can easily install one on your local machine, though – Rory McCrossan Nov 20 '17 at 14:31
  • you need to make sure your server is serving the file... try doing this http://servername/sample.txt in browser and see the result – sumeet kumar Nov 20 '17 at 14:32

1 Answers1

2

If you are running locally this can cause errors: Error while running locally.

See this question for more details

Running the HTML code using XAMPP and calling localhost it works without error. JQuery.get... is not working using file:/// protocol.

Tobias Schäfer
  • 1,330
  • 2
  • 17
  • 35