I was reading w3schools tutorials for ajax and that url really bothered me. Where did they get that??I copied the sample code for ajax that w3schools have given but it doesnt work. I think its because of that url (demo_get.asp).. here is the code that i copied from w3schools.
<!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","demo_get.asp",true);
xmlhttp.send();
}
</script>
</head>
<body>
<h2>AJAX</h2>
<button type="button" onclick="loadXMLDoc()">Request data</button>
<div id="myDiv"></div>
</body>
</html>
I was reading w3schools tutorials for ajax and that url really bothered me. Where did they get that??I copied the sample code for ajax that w3schools have given but it doesnt work. I think its because of that url (demo_get.asp).. here is the code that i copied from w3schools.
<!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","demo_get.asp",true);
xmlhttp.send();
}
</script>
</head>
<body>
<h2>AJAX</h2>
<button type="button" onclick="loadXMLDoc()">Request data</button>
<div id="myDiv"></div>
</body>
</html>
Share
Improve this question
edited Jul 9, 2013 at 8:37
MrCode
64.6k10 gold badges92 silver badges113 bronze badges
asked Jul 9, 2013 at 8:33
jmjassy27jmjassy27
871 gold badge5 silver badges10 bronze badges
1 Answer
Reset to default 5demo_get.asp
is just the name of a file on the server that this AJAX example is reading. Just like any other url, you need to change it to match your code structure. For example, if you need to AJAX load html page mypage.html
, then that's what you need to put into the URL.
Also, unless you have good reason not too, you may be better off using a javascript library, for example, jQuery, which simplifies ajax a lot. In jQuery you can simply do:
$.get('mypage.html', function(data) {
$('#targetdiv').html(data);
});
to load the content of mypage.html
into the div
with id targetdiv
.