According to w3schools the open method of the XMLHttpRequest object takes three parameters: 1. method 2. url 3. a boolean indicating whether the call is asynchronous or not
One of the examples used a text file for the url parameter. I copied the code and replaced the text file with my own and nothing happened. What am I missing?
This is the code I copied:
<!DOCTYPE html>
<html>
<head>
<script>
function loadXMLDoc(){
var xmlhttp;
if (window.XMLHttpRequest){
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else{
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","ajax_info.txt",true);
xmlhttp.send();
}
</script>
</head>
<body>
<div id="myDiv"><h2>Let AJAX change this text</h2></div>
<button type="button" onclick="loadXMLDoc()">Change Content</button>
</body>
</html>
According to w3schools. the open method of the XMLHttpRequest object takes three parameters: 1. method 2. url 3. a boolean indicating whether the call is asynchronous or not
One of the examples used a text file for the url parameter. I copied the code and replaced the text file with my own and nothing happened. What am I missing?
This is the code I copied:
<!DOCTYPE html>
<html>
<head>
<script>
function loadXMLDoc(){
var xmlhttp;
if (window.XMLHttpRequest){
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else{
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","ajax_info.txt",true);
xmlhttp.send();
}
</script>
</head>
<body>
<div id="myDiv"><h2>Let AJAX change this text</h2></div>
<button type="button" onclick="loadXMLDoc()">Change Content</button>
</body>
</html>
Share
Improve this question
edited Oct 5, 2021 at 9:51
Abdullah Akhtar
5585 silver badges14 bronze badges
asked Sep 22, 2014 at 8:26
MR1971MR1971
191 gold badge1 silver badge4 bronze badges
6 Answers
Reset to default 4You should put it inside some server and try.As we are using GET request here so it needed server. I have tried putting it inside wamp its working for me.
XMLHttpRequest is an Http Request :O
It's not called XMLLocalFileRequest :)
try using http://localhost/ajax_info.txt
as a url .. a filename by itself is not a url.
Have you placed file ajax_info.txt
in directory ?
I edit a file as ajax_info.txt
in directory with following content and it works perfectly.
<h1>AJAX</h1>
<p>AJAX is not a programming language.</p>
<p>AJAX is a technique for accessing web servers from a web page.</p>
<p>AJAX stands for Asyncronous JavaScript And XML.</p>
ajax_info.txt file present in w3school server. So thats why you cant access through it using your program.
Look this url: [https://www.w3schools./js/ajax_info.txt]
You need to use local server.
I use xampp as my local server, so I will try to explain in respect of that. If you are using some other local server check for that on google..
Put your files in a folder named (jscalls) in (htdocs) folder of xampp installed on your local machine. Then call
localhost/jscalls/nameOfFile.html
Now when you will click on the button it will get the content in the specified id.
For security reasons, modern browsers do not allow access across domains.
This means that both the web page and the XML file it tries to load, must be located on the same server.
The examples on W3Schools all open XML files located on the W3Schools domain.
If you want to use the example above on one of your own web pages, the XML files you load must be located on your own server.