Fetch data from text file - AJAX and Javascript
Table of Content:
Fetch data from text file - AJAX and Javascript
HTML Code
Hello, world! Hello, world!
This is demo Div
JAVASCRIPT Code
function loadDoc() { const xhttp = new XMLHttpRequest(); xhttp.onload = function() { document.getElementById("demo").innerHTML = this.responseText; } xhttp.open("GET", "textfile.txt"); xhttp.send(); }
All modern browsers support the XMLHttpRequest object.
The XMLHttpRequest object can be used to exchange data with a web server behind the scenes. This means that it is possible to update parts of a web page, without reloading the whole page.
A callback function is a function passed as a parameter to another function. In this case, the callback function should contain the code to execute when the response is ready.
xhttp.onload = function() { // What to do when the response is ready }